Source Code. Projects. Nerd Stuff. Art Stuff.

Many Objects


Posted on October 9th, by amoeboar in classes, icm. No Comments

Making use of objects and ArrayLists.

Class BaseShape sets the parameters for all shapes, which contains movement properties and contains a “Bouncing Shape” check for walls. All other shapes extend BaseShape. In the main tab, the shapes are created in an ArrayList, and are drawn automatically depending on what shape they are, a concept known as polymorphism.

 

[gn_spoiler title="manyObjects source code:" open="0" style="1"]

[gn_tabs style="3"]

[gn_tab title="manyObjects"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/********************************
/ John Capogna
/ manyObjects.pde
/ ITP, 2012
********************************/

ArrayList shapes;

void setup() {
  size(640, 480);
 
  // Initialize the ArrayList
  shapes = new ArrayList();
 
  for (int i = 0; i < 100; i++) {
    if (i < 33) {
      Circle makeCircle = new Circle(30, random(width), random(height), color(255, 0, 0));
      makeCircle.speed.set(0, -1, 0);
      shapes.add(makeCircle);
    } else if (i < 66) {
      Rectangle makeRect = new Rectangle(random(500), random(500), random(10, 50), random(10, 50), color(0, 0, 255));
      makeRect.speed.set(-1, 0, 0);
      shapes.add(makeRect);
    } else {
      Square makeSquare = new Square(random(width), random(height), 10, color(0, 255, 0));
      makeSquare.speed.set(random(1, 3), random(1, 3), 0);
      shapes.add(makeSquare);
    }
  }
}

void draw() {
  background(0);
  // We go through the shapes ArrayLIst
  for (int i = 0; i < shapes.size(); i++) {
    // We cast every object we pull out as a BaseShape.  This works, because all of the
    // shapes we have extend BaseShape
    BaseShape shape = (BaseShape)shapes.get(i);
    // When we call shape.draw, it calls the Circle class's draw function if the
    // shape is a circle the Rectangle class's draw function if the shape is a rectangle
    // the Square's class's draw function if the shape is a Square
    shape.draw();
  }
}
[/gn_tab] [gn_tab title="BaseShape"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class BaseShape {

  PVector position;  // replaces x and y coordinates
  PVector speed;
  PVector acceleration;

  color c;

  BaseShape(PVector position,
  float speedX, float speedY,
  float accelX, float accelY,
  color c) {
    this.position = position;
    speed = new PVector(speedX, speedY);
    acceleration = new PVector(accelX, accelY);
    this.c = c;
  }


  void draw() {
    fill(c);
    speed.add(acceleration);
    position.add(speed);

    // "Bouncing Shape" check
    if (position.x < 0 || position.x > width) {
      speed.x *= -1;
    }

    if (position.y < 0 || position.y > height) {
      speed.y *= -1;
    }
  }
}
[/gn_tab] [gn_tab title="Circle"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Circle extends BaseShape {

  float d;  // diameter
  // Constructor: make a spot in memory, set up objects
  Circle(float d, float x, float y) {
    super(new PVector(x, y), 0, 0, 0, 0, color(0));
    this.d = d;
  }

  // Constructor: four parameters
  Circle(float d, float x, float y, color c) {
    super(new PVector(x, y), 0, 0, 0, 0, color(c));
    this.d = d;
  }

  // draw() directly within Circle
  // BaseShape draws speed, acceleration, color
  void draw() {
    super.draw();
    ellipse(position.x, position.y, d, d);
  }
}
[/gn_tab] [gn_tab title="Rectangle"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Rectangle extends BaseShape {

  PVector size;

  // From BaseShape: (position, speedX, speedY, accelX, accelY, color c)
  Rectangle(float x, float y, float w, float h) {
    // Can set speed PVector (contained in BaseShape) to give this shape
    // motion by calling speed.set in main draw funciton. Here, the values
    // are defaulted to 0
    super(new PVector(x, y), 0, 0, 0, 0, color(0, 0, 0));
    size = new PVector(w, h);
  }

  // Constructor: make a spot in memory, set up the objects
  Rectangle(float x, float y, float w, float h, color c) {
    super(new PVector(x, y), 0, 0, 0, 0, color(c));
    size = new PVector(w, h);
  }

  // draw() directly within Rectangle
  // BaseShape draws speed, acceleration, color
  void draw() {
    super.draw();
    rect(position.x, position.y, size.x, size.y);
  }
}
[/gn_tab] [gn_tab title="Square"]
1
2
3
4
5
6
7
8
9
10
11
12
13
class Square extends Rectangle {

  Square(float x, float y, float size) {
    super(x, y, size, size, color(0));
  }

  // Constructor: make a spot in memory, set up the objects
  Square(float x, float y, float size, color c) {
    super(x, y, size, size, c);
  }
}

  // draw() method contained in Rectangle
[/gn_tab] [/gn_tabs] [/gn_spoiler]




Leave a Reply