Tut­o­r­i­a­l 14: Obj­e­c­t­s and Cla­s­s­e­s

by Lisa Deng
  • Show Sketch
/** @peep sketchcode */
 
void setup() {
  size(300, 300);
  colorMode(HSB, 360, 100, 100);
  squares = new Square[100];
  triangles = new Triangle[50];
  circles = new Circle[50];
  for (int i = 0; i < squares.length; i++) {
    squares[i] = new Square(random(width), random(height),random(10,20),random(10,40),color(random(360), 100, 100));
  }
  for (int i = 0; i < triangles.length; i++) {
    triangles[i] = new Triangle(random(width), random(height),color(random(360), 100, 100));
  } 
  for (int i = 0; i < circles.length; i++) {
    circles[i] = new Circle(random(width), random(height),random(5),color(random(360), 100, 100));
  } 
}
 
void draw() {
  noStroke();
  for (int i = 0; i < squares.length; i++) {
    squares[i].draw();
  }
  for (int i = 0; i < triangles.length; i++) {
    triangles[i].draw();
  }
  for (int i = 0; i < circles.length; i++) {
    circles[i].draw();
  }
}
 
class Square {
  float x;
  float y;
  float w;
  float h;
  color colour;
 
  Square(float _x, float _y, float _w, float _h, float _colour){
    x= _x;
    y= _y;
    h = _h;
    w = _w;
    colour = _colour;
  }
 
  void draw() {
    noStroke();
    fill(colour);
    rect(x,y,w,w)
    fill(100,100,100);
  }
}
 
class Triangle {
  float x;
  float y;
  float x2;
  float y2;
  float x3;
  float y3;
  color colour;
 
  Triangle(float _x, float _y, float _colour){
    x= _x;
    y= _y;
    x2 = _x+10;
    y2 = _y;
    x3 = _x+5;
    y3 = _y-10;
    colour = _colour;
  }
 
  void draw() {
    noStroke();
    fill(colour);
    triangle(x,y,x2,y2,x3,y3);
  }
}
 
class Circle {
  float x;
  float y;
  float r;
  color colour;
 
  Circle(float _x, float _y, float _r, float _colour){
    x= _x;
    y= _y;
    r = _r;
    colour = _colour;
  }
 
  void draw() {
    noStroke();
    fill(colour);
    ellipse(x,y,r*2,r*2);
  }
}

Comments

Nobody has said anything yet.