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 › A tree structure
Page Index Toggle Pages: 1
A tree structure (Read 818 times)
A tree structure
Mar 17th, 2009, 4:52am
 
Hello everyone,there's an urgent problem for me now.
My teacher offered me an exercise to make a interactive scene,where I got a rectangle at the top by default,it flashes when being placed on by a cursor and give birth of another rectangle when clicked,and the new born rectangle have the same interactive property like its mother object.
I see there would be a tree structure guiding behind,and how can I accomplish that?Can you give a simple example?Thank you in advance.
Re: A tree structure
Reply #1 - Mar 17th, 2009, 4:58am
 
I saw that you posted the same message twice. Please don't do that. I removed one of the posts.
Re: A tree structure
Reply #2 - Mar 17th, 2009, 6:56am
 
I'm terribly sorry,but I mean no insult,I'm somehow upset.
I shouldn't occupy the chance of others to be helped.
I'm regretful,forgive me.
Re: A tree structure
Reply #3 - Mar 17th, 2009, 12:40pm
 
No need to profusely apologize, we all make mistakes. Smiley

Your intuition of tree structure isn't wrong, but it is not a tree in a Java Collection sense, rather in the class inheritance sense.

I don't know what you have learned in your course, I hope I won't go to unseen concepts. But to do what you were requested, you should make a Rectangle class with the described behavior. When clicked, it will just create another instance of the class (with different location).
Re: A tree structure
Reply #4 - Mar 17th, 2009, 5:52pm
 
To expand upon PhiLo's suggestion:

Yes, make a rectangle class.  It should hold the (x,y) location of the top-left corner of the rectangle, as well as the width and height of the rectangle.  It should have a method which, when given a pair of mouse coordinates, returns true or false if the mouse coordinates lie inside the rectangle:

boolean hitTest(float x, float y) {
 // it's your classwork, you fill in this part
}

It should also have a method that creates a new rectangle:

Rectangle makeNewRectangle() {
 // you fill in this part
}

The main part of your program will have an ArrayList to hold all of the rectangles.  You will implement the mousePressed() routine in such a way that when the user clicks the mouse, you check each Rectangle in the ArrayList to see whether the mouse was within that rectangle.  If it was, then you can call that rectangle's makeNewRectangle() method.  It will return a new rectangle, which you add to the ArrayList.

Your draw() routine will simply iterate over the ArrayList and call each Rectangle object's draw() method (which you should also implement, probably in terms of Processing's native rect() function).  In your draw() routine, you can also use the hitTestmethod on each rectangle to see whether the mouse is presently inside that rectangle, so as to draw it "plain" if the mouse isn't inside, or "flashing" if it is.
Page Index Toggle Pages: 1