Source Code. Projects. Nerd Stuff. Art Stuff.

Advanced Objects, Polymorphism, Array Lists


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

[gn_tabs style="3"]

[gn_tab title="polymorphism"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Declare a BaseShape variable, named myShape
BaseShape myShape;

void setup() {
  size(640, 480);

  //  You can make myShape a Circle
  myShape = new Circle(10, width/2, height/2, color(255, 0, 0));

  //  Or you can make myShape a Rectangle
  //  myShape = new Rectangle(100, 50, 10, 20);

  //  Or you can make myShape a Square
  //  myShape = new Square(50, 400, 400);

  //    You can make myShape any of these objects because they all extend BaseShape
}

void draw() {

  //  Then when you call myShape.draw(), it will draw the appropriate shape type,
  //  because the object itself knows how it should be rendering
  myShape.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
class BaseShape {

  float x;
  float y;
  color c;

  BaseShape(float _x, float _y) {
    x = _x;
    y = _y;
    c = color(255);
  }

  //Constructor: make a spot in memory, set up the objs
  BaseShape(float _x, float _y, color _c) {
    x = _x;
    y = _y;
    c = _c;
  }

  void draw() {
    fill(c);
  }
}
[/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
23
24
25
26
27
28
29
class Circle extends BaseShape {

  float d;
  float x;
  float y;
  color c;

  Circle(float _d, float _x, float _y) {
    super(_x, _y, color(255));
    d = _d;
    x = _x;
    y = _y;
    c = color(255);
  }

  //Constructor: make a spot in memory, set up the objs
  Circle(float _d, float _x, float _y, color _c) {
    super(_x, _y, _c);
    d = _d;
    x = _x;
    y = _y;
    c = _c;
  }

  void draw() {
    fill(c);
    ellipse(x, 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
class Rectangle extends BaseShape {

  float w;
  float h;

  Rectangle(float _w, float _h, float _x, float _y) {
    super(_x, _y, color(255));
    w = _w;
    h = _h;
  }

  //Constructor: make a spot in memory, set up the objs
  Rectangle(float _w, float _h, float _x, float _y, color _c) {
    super(_x, _y, _c);
    w = _w;
    h = _h;
  }

  void draw() {
    super.draw();
    rect(x, y, w, h);
  }
}
[/gn_tab] [gn_tab title="Square"]
1
2
3
4
5
6
7
8
9
10
11
class Square extends Rectangle {

  Square(float _w, float _x, float _y) {
    super(_w, _w, _x, _y, color(255));
  }

  //Constructor: make a spot in memory, set up the objs
  Square(float _w, float _x, float _y, color _c) {
    super(_w, _w, _x, _y, _c);
  }
}
[/gn_tab] [/gn_tabs] [/gn_spoiler]

 
 


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

[gn_tabs style="3"]

[gn_tab title="arrayList"]

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
//An ArrayList is like an Array, here we declare an ArrayList with the name shapes
ArrayList shapes;

void setup() {
  size(640, 480);

  //We initialize the ArrayList
  shapes = new ArrayList();

  //Here we put 100 objects; Cirles, Rectangles, and Squares into the shapes ArrayList
  for (int i = 0; i < 100; i++) {
    if (i < 25) {
      shapes.add(new Circle(10, width/random(100), height/random(100), color(255, 0, 0)));
    }
    else if (i < 75) {
      shapes.add(new Rectangle(10, 20, random(width), random(height)));
    }
    else {
      shapes.add(new Square(50, random(width), random(height), color(0, 0, 255)));
    }
  }
}

void draw() {

  //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 all 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
class BaseShape {

  float x;
  float y;
  color c;

  BaseShape(float _x, float _y) {
    x = _x;
    y = _y;
    c = color(255);
  }

  //Constructor: make a spot in memory, set up the objs
  BaseShape(float _x, float _y, color _c) {
    x = _x;
    y = _y;
    c = _c;
  }

  void draw() {
    fill(c);
  }
}
[/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
23
24
25
26
27
28
29
class Circle extends BaseShape {

  float d;
  float x;
  float y;
  color c;

  Circle(float _d, float _x, float _y) {
    super(_x, _y, color(255));
    d = _d;
    x = _x;
    y = _y;
    c = color(255);
  }

  //Constructor: make a spot in memory, set up the objs
  Circle(float _d, float _x, float _y, color _c) {
    super(_x, _y, _c);
    d = _d;
    x = _x;
    y = _y;
    c = _c;
  }

  void draw() {
    fill(c);
    ellipse(x, 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
class Rectangle extends BaseShape {

  float w;
  float h;

  Rectangle(float _w, float _h, float _x, float _y) {
    super(_x, _y, color(255));
    w = _w;
    h = _h;
  }

  //Constructor: make a spot in memory, set up the objs
  Rectangle(float _w, float _h, float _x, float _y, color _c) {
    super(_x, _y, _c);
    w = _w;
    h = _h;
  }

  void draw() {
    super.draw();
    rect(x, y, w, h);
  }
}
[/gn_tab] [gn_tab title="Square"]
1
2
3
4
5
6
7
8
9
10
11
class Square extends Rectangle {

  Square(float _w, float _x, float _y) {
    super(_w, _w, _x, _y, color(255));
  }

  //Constructor: make a spot in memory, set up the objs
  Square(float _w, float _x, float _y, color _c) {
    super(_w, _w, _x, _y, _c);
  }
}
[/gn_tab] [/gn_tabs] [/gn_spoiler]