Having decided upon a content layout I have started to think about the various bits of code of will need for each area. After sometime contemplating how all these bits of code should fit together to form the final program I decided that perhaps a better approach would be to create the final program first and then I can use elements of this code with my core text to demonstrate certain aspects, for example the default constructor. My program is now complete and it demonstrates a default constructor, constructors with arguments and the 2 ways of using the this keyword, by producing multiple instances of my Frog class. Now to get on with the text.
The code:
void setup() {
size(640, 480);
background(255, 248, 220);
smooth();
noLoop();
}
void draw() {
Frog freddo;
for (int i = 0; i < 75; i++) {
switch (int(random(0, 6)+1)) {
case 1:
freddo = new Frog();
break;
case 2:
freddo = new Frog(int(random(52, 588)+1),
int(random(37, 442)+1));
break;
case 3:
freddo = new Frog(int(random(0, 254)+1),
int(random(0, 254)+1),
int(random(0, 254)+1));
break;
default:
freddo = new Frog(int(random(52, 588)+1),
int(random(37, 442)+1),
int(random(0, 254)+1),
int(random(0, 254)+1),
int(random(0, 254)+1));
break;
}
freddo.display();
}
}
class Frog {
// properties
int xPos;
int yPos;
int redColBody;
int grnColBody;
int bluColBody;
// constructors
// default constructor
Frog() {
this(320, 240, 0, 128, 0);
}
// constructor (position)
Frog(int xPos, int yPos) {
this(xPos, yPos, 244, 164, 96);
}
// constructor (coloured body)
Frog(int redColBody, int grnColBody, int bluColBody) {
this(320, 240,redColBody, grnColBody, bluColBody);
}
// constructor (position & coloured body)
Frog(int xPos, int yPos, int redColBody, int grnColBody,
int bluColBody) {
this.xPos = xPos;
this.yPos = yPos;
this.redColBody = redColBody;
this.grnColBody = grnColBody;
this.bluColBody = bluColBody;
}
// methods
void display() {
stroke(0);
strokeWeight(1);
// body
fill(redColBody, grnColBody, bluColBody);
ellipse(xPos, yPos, 90, 45);
// feet
fill(107, 142, 35);
ellipse(xPos - 30, yPos + 15, 45, 23);
ellipse(xPos + 30, yPos + 15, 45, 23);
// eyes
fill(255, 255, 255);
ellipse(xPos - 15, yPos - 23, 23, 30);
ellipse(xPos + 15, yPos - 23, 23, 30);
fill(0, 0, 0);
ellipse(xPos - 15, yPos - 23, 8, 8);
ellipse(xPos + 15, yPos - 23, 8, 8);
// mouth
noFill();
strokeWeight(2);
arc(xPos, yPos - 2, 30, 5, 0, PI);
}
}