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 › changed example to OOP no longer works
Page Index Toggle Pages: 1
changed example to OOP no longer works (Read 716 times)
changed example to OOP no longer works
Aug 29th, 2009, 10:07am
 
Original example from the Learning Processing book. There is a prompt to try this out in an object oriented manor. Which is the lower section of code that I did.
Code:

// Learning Processing
// Daniel Shiffman

// Example 9-8: A snake following the mouse

// Declare two arrays with 50 elements.
int[] xpos = new int[50];
int[] ypos = new int[50];

void setup() {
 size(200,200);
 
 smooth();
 // Initialize all elements of each array to zero.
 for (int i = 0; i < xpos.length; i ++ ) {
   xpos[i] = 0;
   ypos[i] = 0;
 }
}

void draw() {
 background(255);
 
 // Shift array values
 for (int i = 0; i < xpos.length-1; i ++ ) {
   // Shift all elements down one spot.
   // xpos[0] = xpos[1], xpos[1] = xpos = [2], and so on. Stop at the second to last element.
   xpos[i] = xpos[i+1];
   ypos[i] = ypos[i+1];
 }
 
 // New location
 xpos[xpos.length-1] = mouseX; // Update the last spot in the array with the mouse location.
 ypos[ypos.length-1] = mouseY;
 
 // Draw everything
 for (int i = 0; i < xpos.length; i ++ ) {
    // Draw an ellipse for each element in the arrays.
    // Color and size are tied to the loop's counter: i.
   noStroke();
   fill(255-i*5);
   ellipse(xpos[i],ypos[i],i,i);
 }
}

My code that just shows one black ellipse that follows the mouse instead of showing a tail.
Code:

Point[] points = new Point[50];

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

 smooth();

 for (int i = 0; i < points.length; i ++ ) {
   points[i] = new Point(0,0);
 }

}

void draw() {
 background(255);


 for (int i = 0; i < points.length-1; i ++ ) {
   points[i] = points[i+1];
 }

 points[points.length-1].setXpos(mouseX);
 points[points.length-1].setYpos(mouseY);
 
 for (int i = 0; i < points.length; i ++ ) {
   noStroke();
   fill(255-i*5);
   ellipse(points[i].getXpos(),points[i].getYpos(),i,i);
 }


}
//////////////////////////////// my Point class below
class Point {
 int ypos, xpos;

 Point(int x, int y) {
   xpos = x;
   ypos = y;
 }
 
 int setXpos(int x) {
   xpos = x;
   return xpos;
 }
 
 int setYpos(int y) {
   ypos = y;
   return ypos;
 }
 
 int getXpos() {
   return xpos;
 }
 
 int getYpos() {
   return ypos;
 }

}
Re: changed example to OOP no longer works
Reply #1 - Aug 29th, 2009, 12:43pm
 
this is the solution i came up with:

Code:
Point points;

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

smooth();

points = new Point(50);

}

void draw() {
background(255);


points.shift();

points.setXpos(mouseX);
points.setYpos(mouseY);

for (int i = 0; i < 50; i ++ ) {
noStroke();
fill(255-i*5);
ellipse(points.getXpos(i),points.getYpos(i),i,i);
}


}
//////////////////////////////// my Point class below
class Point {
int[] ypos, xpos;
int numPoints;

Point(int nP) {
numPoints = nP;
xpos = new int[numPoints];
ypos = new int[numPoints];
}

void shift()
{
for(int i = 0; i < numPoints-1; i++)
{
xpos[i] = xpos[i+1];
ypos[i] = ypos[i+1];
}
}

void setXpos(int x) {
xpos[numPoints-1] = x;
}

void setYpos(int y) {
ypos[numPoints-1] = y;
}

int getXpos(int index) {
return xpos[index];
}

int getYpos(int index) {
return ypos[index];
}

}
Re: changed example to OOP no longer works
Reply #2 - Aug 29th, 2009, 1:10pm
 
another possibility that is much simpler would be correcting the for iteration you use to shift values insides the points[] array. the way i understand it, if you use points[i] = points[i+1] you don't actually shift the values of x and y as you should for the example to work properly.

substituting the for iteration in your code for the following iteration solves the problem, once it specifically shifts values of x and y between the instances of Point.

Code:
  for (int i = 0; i < points.length-1; i ++ ) {
points[i].setXpos(points[i+1].xpos);
points[i].setYpos(points[i+1].ypos);
}


hope this helps.
Re: changed example to OOP no longer works
Reply #3 - Aug 29th, 2009, 1:58pm
 
Thank you so much.

Exactly the explanation I was looking for. It makes sense that the state (xpos, and ypos) of my Point object weren't changing properly. I was just thinking about it wrong when I was trying to adapt the solution over to something a little more object oriented.

Your solution is very nice, although I feel like it should be called PointManager or something because it is much more sophisticated than a  "Point". Anyway, thanks again for the time and help.
Page Index Toggle Pages: 1