This code calls for various remarks, some minor, others related to your issue...
- You should use boolean instead of Boolean. The former is a primitive type, small and fast. The latter is an object, fatter and slower. No difference in your code, but later it can haunt you back...
- No need to test the value of a boolean with an equality, since it can have only two values.
- The PressButton method name is a bit misleading, suggesting an action where it is a test. I suggest something like Button1.IsPressed() or similar.
- Note: since these are methods, ie. related to the class they belong to, there is no need to repeat the name of the class in the method names. That's a matter of preferences, so that's just a suggestion, of course.
And more importantly, your method of communication between two objects is interesting, but won't work: when you put a variable name as parameter, you pass only its value, not a reference to the variable. In Java, there is no (simple) way to pass such reference.
There is (at least) two ways to solve this.
Either you pass a reference of Button1 to Indicator1, so the latter can check itself the state of Button1.
Or you pass directly the state of Button1 to the display method of Indicator1.
It depends if Indicator1 needs more information from Button1 and if you want a strong coupling, or if you prefer to have the coupling at higher level.
I show below a working code using the second usage and the suggested fixes.
Code:Indicator Indicator1;
Button Button1;
void setup() {
size(500,500);
PFont font;
//~ font=loadFont("Tahoma-12.vlw");
font=createFont("Tahoma", 12);
textFont(font);
Button1= new Button(50,50,60,25,"Button1",57,67);
Indicator1= new Indicator(120,50,50,25,"Indicator1");
}
void draw() {
background(150);
Button1.Display();
Indicator1.Display(Button1.IsPressed());
}
class Button{
float Xpos;
float Ypos;
float Length;
float Height;
String Tekst;
float XTekst;
float YTekst;
boolean mouseOverRect() {
return ((mouseX >= Xpos) && (mouseX <= Xpos + Length) && (mouseY >= Ypos) && (mouseY <= Ypos + Height));
}
Button(float tempXpos, float tempYpos, float tempLength, float tempHeigth, String tempTekst, float tempXTekst, float tempYTekst){
Xpos=tempXpos;
Ypos=tempYpos;
Length=tempLength;
Height=tempHeigth;
Tekst=tempTekst;
XTekst=tempXTekst;
YTekst=tempYTekst;
}
void Display() {
if (mouseOverRect()) {
fill(0,255,255);
}
else {
fill(97,118,219);
}
stroke(97,118,150);
rect(Xpos,Ypos,Length,Height);
fill(0,18,106);
text(Tekst,XTekst,YTekst);
}
boolean IsPressed() {
// Note boolean test before method call:
// it is faster is mouse isn't pressed
return mousePressed && mouseOverRect();
}
}
class Indicator{
float Xpos;
float Ypos;
float Length;
float Height;
String Text;
Indicator(float tempXpos, float tempYpos, float tempLength, float tempHeigth, String tempText){
Xpos=tempXpos;
Ypos=tempYpos;
Length=tempLength;
Height=tempHeigth;
Text=tempText;
}
void Display(boolean indicatorOn) {
if (indicatorOn){
fill(0,255,255);
}
else {
fill(188,193,219);
}
stroke(97,118,150);
rect(Xpos,Ypos,Length,Height);
fill(0,18,106);
text(Text,Xpos,Ypos-5);
}
}