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 › square object
Page Index Toggle Pages: 1
square object (Read 657 times)
square object
Mar 16th, 2009, 6:23pm
 
Hi, I'm pretty new to Processing and so far I quite like it, at the moment I'm working on a project and my goal is to parse a text file, for each parsed line I'm drawing a square on the canvas, at a later time I'm supposed to modify these squares. The parsing works fine and also the drawing is ok.
I was looking for a way to generate the square as an object,  in order to store them somehow and modify them later on, up to now I simply use the rect(x,y, width, height). Anyone has a suggestion?  Thanks
Re: square object
Reply #1 - Mar 16th, 2009, 6:56pm
 
just define your own class. it'll be very simple.

http://processing.org/reference/class.html
Re: square object
Reply #2 - Mar 16th, 2009, 9:16pm
 
Yes. Something like:

class Square
{
 float x, y, w, h; // x,y location, w,h size.
 Square() { x = y = width = height = 0.0f; } // default constructor.
 Square(float X, float Y, float W, float H)
 {
   x = X;
   y = Y;
   w= W;
   h = H;
 }
 drawMe() // default drawing code.
 {
   rect(x,y,width,height);
 }
 drawMe(PGraphics g)
 {
   g.rect(x,y,width,height);
 }
}

Construct one such object for each line you parse out of the file, and store them in an ArrayList.  When you need to, modify each one's x,y,w,h values.  Your draw() routine then gets pretty simple:

void draw()
{
 background(0); // or whatever.
 for(int i = 0; i < ListOfSquare.size(); i++)
   (Square)(ListOfSquares.get(i)).drawMe();
}
Re: square object
Reply #3 - Mar 20th, 2009, 2:19pm
 
Thank you guys, seems a good idea. I'm trying to make it work with the rest. Thanks again.
Page Index Toggle Pages: 1