자유게시판

Lv. 82 프로그래머 프로토스 작성자 게시물 더보기
/**
 * Inheritance
 * 
 * A class can be defined using another class as a foundation. In **-oriented
 * programming terminology, one class can inherit fi elds and methods from another. 
 * An ** that inherits from another is called a subclass, and the ** it 
 * inherits from is called a superclass. A subclass extends the superclass.
 */

SpinSpots spots, spots2;
SpinArm arm, arm2;
SpinBox box;

void setup() {
  size(640, 360);
  arm = new SpinArm(width/2, height/2, 0.01);
  spots = new SpinSpots(width/2, height/2, -0.02, 90.0);
  
  arm2 = new SpinArm(width/4, height/4, -0.02);
  spots2 = new SpinSpots(width*3/4, height*3/4, 0.04, 50.0);
  
  box = new SpinBox(width*3/4, height*1/4, 0.05, 80, 40);
}

void draw() {
  background(204);
  arm.update();
  arm.display();
  spots.update();
  spots.display();
  
  arm2.update();
  arm2.display();
  spots2.update();
  spots2.display();

  box.update();
  box.display();
}

abstract class Spin {
  float x, y, speed;
  float angle = 0.0;
  Spin(float xpos, float ypos, float s) {
    x = xpos;
    y = ypos;
    speed = s;
  }
  void update() {
    angle += speed;
  }
  
  void display() {
    strokeWeight(1);
    stroke(0);
    fill(255);
    pushMatrix();
    translate(x, y);
    angle += speed;
    rotate(angle);
    
    draw();
    
    popMatrix();
  }
  
  abstract void draw();
}

class SpinArm extends Spin {
  SpinArm(float x, float y, float s) {
    super(x, y, s);
  }
  
  void draw() {
    line(0, 0, 165, 0);
  }
}

class SpinSpots extends Spin {
  float dim;
  SpinSpots(float x, float y, float s, float d) {
    super(x, y, s);
    dim = d;
  }
  
  void draw() {
    noStroke();
    ellipse(-dim/2, 0, dim, dim);
    ellipse(dim/2, 0, dim, dim);
  }
}

class SpinBox extends Spin {
  float width;
  float height;
  
  SpinBox(float x, float y, float s, float w, float h) {
    super(x, y, s);
    width = w;
    height = h;
  }
  
  void draw() {
    noStroke();
    rectMode(CENTER);
    fill(100);
    rect(0, 0, width, height);
  }
}
댓글을 남기시려면 로그인 해주세요
댓글 : 0