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 & HelpSyntax Questions › Did I convert data into a class correctly
Page Index Toggle Pages: 1
Did I convert data into a class correctly (Read 1322 times)
Did I convert data into a class correctly
Aug 29th, 2009, 11:30am
 
I have converted data into functions, objects and a class, but I don't understand where the mouseClicked function fits into this class. It seems to belong to two objects. Can someone show me? thanks.

Assembly assembly;

boolean button = false;

void setup() {
size(200,200);
assembly = new Assembly(20,20,100,20,30);
}

void draw() {
background(255);
assembly.display_urect();
assembly.display_bdoor();
assembly.display_brect();
assembly.move_door();
assembly.mouseClicked();
}

class Assembly {
 
 int x,y,w,h,d;
 
 Assembly(int tempx, int tempy, int tempw, int temph, int tempd) {
   x = tempx;
   y = tempy;
   w = tempw;
   h = temph;
   d = tempd;
 }
 
 void display_urect() {
 stroke(0);
 fill (140,160,220);
 if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h)
 fill(150,190,250);
 rect(x,y,w,h);
 }
 
 void display_bdoor() {
   fill(60);
   rect(80,140,30,50);
}

 void display_brect() {
 if (button) {
 stroke(0);
 fill(180,190,250);
 rect(20,60,160,60);
 }
 }
 
 void move_door() {
 if (button) {
 fill(100,150,100);
 rect(80,140,d,50);
 d = 5;
 }
}

void mouseClicked() {
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h    ) {
button = !button;
}
}
}
Re: Did I convert data into a class correctly
Reply #1 - Aug 29th, 2009, 3:23pm
 
A possible way is to keep the global method mouseClicked() (callback function) and there, call the assembly.mouseClicked() method, to handle it.
Re: Did I convert data into a class correctly
Reply #2 - Aug 30th, 2009, 10:50am
 
Thanks for your response.

I am so new at this it is not clear to me what your suggesting.

It sounds like your saying keep mouseClicked() at its current location and call this method from assembly.mouseclicked() in draw().

Is that not what I am already doing. I think maybe I don't understand.
Re: Did I convert data into a class correctly
Reply #3 - Aug 30th, 2009, 11:09pm
 
It is hard to say because your code is missing a bit of indentation, making hard to see what function belongs to what block.
But apparently the mouseClicked() function definition is inside your class.
You call it from draw().
It is better to call it from the PApplet's mouseClicked(), the "real" mouse event handler.
Or, call it from draw() like you do, but only if mousePressed (the variable) is true.
Side note: mouseClicked is actually called after mousePressed() and mouseReleased(), it is a combination of both.
Re: Did I convert data into a class correctly
Reply #4 - Aug 31st, 2009, 4:22am
 
Maybe this confuses the issue further, but Quark posted a really neat example of implementing mouse controls in a class.

(As a sidenote I ran into trouble when trying to registerDraw() on two different classes, but haven't had time to look into a solution to that yet.  Turns out it's just a matter of explicitly declaring classes as 'public'.)
Re: Did I convert data into a class correctly
Reply #5 - Aug 31st, 2009, 11:44am
 
I do not know what PApplet's mouseClicked() means. Does the P stand for Processing? Never mind; I just found it in the back of my Learning Processing book in the advanced section. It means parent. They just mention it.

What I am trying to do is an exercise much earlier in the book about converting the content of my program into objects and classes. The example in the book does not involve mouseClicked. If there is not a simple way of dealing with mouseClicked and classes, maybe I should skip it for now until my programing abilities grow a little more.

Thanks blindfish for that link. It looks a little advanced for me at the moment, but I will bookmark it.
Re: Did I convert data into a class correctly
Reply #6 - Aug 31st, 2009, 1:03pm
 
The P of PApplet means Processing...
A PApplet is actually your sketch, sorry for using obscure terms.
A more concrete example:
Code:
Assembly assembly;

boolean button = false;

void setup() {
size(200,200);
assembly = new Assembly(20,20,100,20,30);
}

void draw() {
background(255);
assembly.display_urect();
assembly.display_bdoor();
assembly.display_brect();
assembly.move_door();
}

void mouseClicked() // <-- That's what I meant
{
assembly.mouseClicked();
}
Re: Did I convert data into a class correctly
Reply #7 - Aug 31st, 2009, 2:41pm
 
That worked. Thanks. I don't really understand why.

How did you learn this? A book or website tutorial on processing. Did you already know this coming from another programming language like Java or C++?

I could not find anything in-depth on this site.

The next program I will create will be a menu. It will have mouseover rectangles that light up and when mouseClicked, display a side menu. My difficulties might be converting data and functionality into objects and classes. These mouse events sound like functionality, but I am not sure what class they would belong to if any in a multi-class program.
Re: Did I convert data into a class correctly
Reply #8 - Sep 1st, 2009, 1:45am
 
I mostly learned by looking at reference, and even more looking at examples, those of Processing, and those on this forum.
Beside, yes, I have a good experience of programming, it helps... :-P

How my code works? mousePressed() is a somehow "magical" function, like setup() and draw(): these functions are automatically called by Processing (the PApplet) when needed: setup() at the start, draw() every n milliseconds (depending on frameRate), mousePressed and similar when user does a physical action. These are also called "callback" functions, because the application call them back when an event occurs.

So Processing's mousePressed() is called (it knows only this one, unless you use the register trick above), and assembly.mouseClicked() delegates this event to your class. If you have several class instances, you have to call them all in a loop. Or, again, to register them (and Processing will loop itself to do this job).
Re: Did I convert data into a class correctly
Reply #9 - Sep 2nd, 2009, 12:29am
 
Thanks. It sounds a little complicated for me to understand at this point in time. I will review what you said again later when I have some more experience.

I will study up on that register trick and see if I can make sense out of it.

I will try to experiment with mouseClick and mousePress by making an example program.
Page Index Toggle Pages: 1