We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Using two classes
Page Index Toggle Pages: 1
Using two classes (Read 709 times)
Using two classes
Feb 21st, 2010, 12:32am
 
Hello,

I tried to use two classes in a program. One for a button and one for a indicator that needs to go "high" when the button is pressed. The program runs but when you press the button, the indicator won't go "high". I can't seem te find the problem myself. I made the following code
Code:

Indicator Indicator1;
Button Button1;

Boolean InputIndicator1=false;

void setup() {
size(500,500);

PFont font;
font=loadFont("Tahoma-12.vlw");
textFont(font);

Button1= new Button(50,50,60,25,"Button1",57,67,InputIndicator1);
Indicator1= new Indicator(120,50,50,25,"Indicator1",InputIndicator1);
}

void draw() {
background(150);
Button1.DisplayButton();
Button1.PressButton();
Indicator1.DisplayIndicator();

}

class Button{
float Xpos;
float Ypos;
float Length;
float Height;
String Tekst;
float XTekst;
float YTekst;
Boolean ButtonPress;
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,Boolean tempButtonPress){
Xpos=tempXpos;
Ypos=tempYpos;
Length=tempLength;
Height=tempHeigth;
Tekst=tempTekst;
XTekst=tempXTekst;
YTekst=tempYTekst;
ButtonPress=tempButtonPress;


}

void DisplayButton() {

if (mouseOverRect()== true) {
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);
}

void PressButton() {
if (mouseOverRect() == true) {
if (mousePressed) {
ButtonPress= true;
}
else {
ButtonPress= false;
}
}
}
}
class Indicator{
float Xpos;
float Ypos;
float Length;
float Height;
String Text;
Boolean IndicatorOn;


Indicator(float tempXpos, float tempYpos, float tempLength, float tempHeigth, String tempText, Boolean tempIndicatorOn){
Xpos=tempXpos;
Ypos=tempYpos;
Length=tempLength;
Height=tempHeigth;
Text=tempText;
IndicatorOn=tempIndicatorOn;
}

void DisplayIndicator() {
if (IndicatorOn==true){
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);
}
}







Greetings,
* Willem
Re: Using two classes
Reply #1 - Feb 21st, 2010, 1:12am
 
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... Smiley
- 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);
 }
}
Re: Using two classes
Reply #2 - Feb 23rd, 2010, 1:22am
 
Hi,

Thanks for the help Wink

Greetings,

Willem
Page Index Toggle Pages: 1