Problem: Subclass method does not respond to superclass method
in
Programming Questions
•
2 years ago
When clicked, the white box should change to a randomized color. It works if I put the mouse events (rollover+click) in the subclass display method, but not when I put it in the superclass. I need it to be in the superclass because I will be extending several subclasses that will use this (boxes with images; box with patterns, etc,).
RandomColorPalette myRandomColorPalette;
void setup() { // call the setup function to define initial enviroment properties
size(900, 700); // set the canvas size at 800 pixels by 800 pixels
background(230); // set background color to light gray
myRandomColorPalette=new RandomColorPalette (color(227, 208, 140), 100, 400, 0);
} // end the setup function
void draw () { // call the draw function
background(230); // reset background color to light gray everytime the draw function is recalled
myRandomColorPalette.display();
} // end the draw function
abstract class Grid {
boolean mouseClickEvent=false;
boolean mouseClickOverBox=false;
float sizeOfSquares;
color gridClr;
float boxPosX;
float boxPosY;
Grid(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
} // end of the constructor
void display () {
// initialization of quilt patterns (from Shiffman page 261)
for (int i=0;i<width;i+=sizeOfSquares) {
for (int j=0;j<height;j+=sizeOfSquares) {
fill(gridClr);
rect(i, j, sizeOfSquares, sizeOfSquares);
}
}
} // end of the display function
boolean mouseClickEvent () {
if (mousePressed) {
return true;
}
else {
return false;
}
}
boolean mouseClickOverBox () {
if (mouseClickEvent && ((mouseX>boxPosX) && (mouseX<boxPosX+sizeOfSquares) && (mouseY>boxPosY) && (mouseY<boxPosY+sizeOfSquares))) {
return true;
}
else {
return false;
}
}
}
class RandomColorPalette extends Grid {
float redColorChangeVariable;
float greenColorChangeVariable;
float blueColorChangeVariable;
RandomColorPalette(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
redColorChangeVariable=255.0;
greenColorChangeVariable=255.0;
blueColorChangeVariable=255.0;
}
void display () {
fill (redColorChangeVariable, greenColorChangeVariable, blueColorChangeVariable);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
if (mouseClickOverBox) {
redColorChangeVariable= random(1, 255);
greenColorChangeVariable= random(1, 255);
blueColorChangeVariable= random(1, 255);
fill (redColorChangeVariable, greenColorChangeVariable, blueColorChangeVariable);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
}
}
}
RandomColorPalette myRandomColorPalette;
void setup() { // call the setup function to define initial enviroment properties
size(900, 700); // set the canvas size at 800 pixels by 800 pixels
background(230); // set background color to light gray
myRandomColorPalette=new RandomColorPalette (color(227, 208, 140), 100, 400, 0);
} // end the setup function
void draw () { // call the draw function
background(230); // reset background color to light gray everytime the draw function is recalled
myRandomColorPalette.display();
} // end the draw function
abstract class Grid {
boolean mouseClickEvent=false;
boolean mouseClickOverBox=false;
float sizeOfSquares;
color gridClr;
float boxPosX;
float boxPosY;
Grid(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
} // end of the constructor
void display () {
// initialization of quilt patterns (from Shiffman page 261)
for (int i=0;i<width;i+=sizeOfSquares) {
for (int j=0;j<height;j+=sizeOfSquares) {
fill(gridClr);
rect(i, j, sizeOfSquares, sizeOfSquares);
}
}
} // end of the display function
boolean mouseClickEvent () {
if (mousePressed) {
return true;
}
else {
return false;
}
}
boolean mouseClickOverBox () {
if (mouseClickEvent && ((mouseX>boxPosX) && (mouseX<boxPosX+sizeOfSquares) && (mouseY>boxPosY) && (mouseY<boxPosY+sizeOfSquares))) {
return true;
}
else {
return false;
}
}
}
class RandomColorPalette extends Grid {
float redColorChangeVariable;
float greenColorChangeVariable;
float blueColorChangeVariable;
RandomColorPalette(color tempgridClr, float tempsizeOfSquares, float tempBoxPosX, float tempBoxPosY) {
super (tempgridClr, tempsizeOfSquares, tempBoxPosX, tempBoxPosY);
gridClr=tempgridClr;
sizeOfSquares=tempsizeOfSquares;
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
redColorChangeVariable=255.0;
greenColorChangeVariable=255.0;
blueColorChangeVariable=255.0;
}
void display () {
fill (redColorChangeVariable, greenColorChangeVariable, blueColorChangeVariable);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
if (mouseClickOverBox) {
redColorChangeVariable= random(1, 255);
greenColorChangeVariable= random(1, 255);
blueColorChangeVariable= random(1, 255);
fill (redColorChangeVariable, greenColorChangeVariable, blueColorChangeVariable);
rect (boxPosX, boxPosY, sizeOfSquares, sizeOfSquares);
}
}
}
1