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 › Updating variables on Objects
Page Index Toggle Pages: 1
Updating variables on Objects (Read 3256 times)
Updating variables on Objects
Jun 5th, 2010, 4:16pm
 
Help needed,
on the following code Im trying to detect when the mouse passes over the cell grid, and just change an object variable named ccolor from 0 to 255 of the object instance related to the mouser coordinates.

I tried different things, but non worked as expected.
I maybe interpreting something wrong on how to update variables inside an object.

any comment to redirect my understanding will be appreciated.

thank you

Code:
//la grilla tiene cuadrados externos e internos,
//la diferencia de tamaño entre estos es de 2 veces el espesor de borde definido
//
int numrows = 8;
int numcols=8;
int rectWidthext = 50;
int rectHeightext = 50;
int rectborder=3;
int rectWidth=rectWidthext-2*rectborder;
int rectHeight=rectHeightext-2*rectborder;
// 2D Array of objects
Cell[][] grid;

void setup() {
 size(rectWidthext*numcols+2*rectborder,rectHeightext*numrows+2*rectborder);
 grid = new Cell[numcols][numrows];
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Initialize each object
     grid[i][j] = new Cell(i*50,j*50,50,50,i+j,0);
   }
 }

}

void draw() {
 background(0);
 fill(255);
 // The counter variables i and j are also the column and row numbers and
 // are used as arguments to the constructor for each object in the grid.  
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Oscillate and display each object
     grid[i][j].display();
     //grid[i][j].mouseover();
   }
 }
 
 //for (int y= 0; y<numrows; y ++){
  // for (int x= 0; x<numcols; x ++){
   //  int posx=rectborder+x*rectWidthext;//calcula la posicion del vertice en base al indice de la celda
   //  int posy=rectborder+y*rectHeightext;//idem

   //  rect(posx, posy,rectWidthext, rectHeightext);

    // if (mouseX > posx && mouseX < posx+rectWidthext && //verifica if mouse over
    // mouseY > posy && mouseY < posy+rectHeightext) {
     //  fill(0);//if over lo cambia de color
     //  rect(posx, posy,rectWidthext, rectHeightext);
       //incorporar aca el cambio de estado en una matriz
    // }
   //  fill(255);
   //}
// }

}

// A Cell object
class Cell {
 // A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
 float x,y;   // x,y location
 float w,h;   // width and height
 float angle; // angle for oscillating brightness
 int ccolor;//color de la celda para transmitir 0-255

 // Cell Constructor
 Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle,int tempcolor) {
   x = tempX;
   y = tempY;
   w = tempW;
   h = tempH;
   ccolor = tempcolor;
   angle = tempAngle;

 }
 
 // Oscillation means increase angle
 void oscillate() {
   angle += 0.02;
 }

 void display() {
   stroke(255);
   // Color calculated using sine wave
   //fill(127+127*sin(angle));
   mouseover();
   fill(ccolor);
   rect(x+3,y+3,w,h);
   //print (x+" ");
   
 }
 
 //mouse over detection
 void mouseover() {
   
  if (pmouseX > x && pmouseX < x+w && //verifica if mouse over
     pmouseY > y && pmouseY < y+h) {
      ccolor = 0;//if over lo cambia de color
     print (x+"_"+mouseX+" ");
     }
     ccolor= 100;
 }
}
Re: Updating variables on Objects
Reply #1 - Jun 5th, 2010, 4:36pm
 
you set the color of every cell to 100 in your mouseOver function, no matter what.
Maybe you just miss an else here. if that is what you want to achieve.

void mouseover() {
   
  if (pmouseX > x && pmouseX < x+w && //verifica if mouse over
     pmouseY > y && pmouseY < y+h) {
      ccolor = 0;//if over lo cambia de color
     print (x+"_"+mouseX+" ");
     }else{
     ccolor= 100;}
 }
Re: Updating variables on Objects
Reply #2 - Jun 6th, 2010, 6:57am
 
Yeah, what Cedric says.

But what I'm wondering, is it possible for classes to have their own mouseReleased & mouseMoved listeners? declaring additional mouseReleased() functions inside a class description wouldn't work here.
Re: Updating variables on Objects
Reply #3 - Jun 6th, 2010, 7:24am
 
As it happens I've done something along those lines with the demo I link to from this post (see the ControlPoint class).  IIRC the technique is something Quark mentioned here a while ago and I believe is also in the Hacks section.

Basically you can use 'registerMouseEvent()' on a class (which must be declared public) and then write a custom mouseEvent() method within the class.  If nothing else using this and registerDraw() helps separate class specific code from the main draw loop; which you'll see is pretty much empty in my demo...
Re: Updating variables on Objects
Reply #4 - Jun 6th, 2010, 8:14am
 
That's nicely written. MouseEvent looks very useful. Will have a go at it.

Impressive devil, btw.  Smiley
Re: Updating variables on Objects
Reply #5 - Jun 6th, 2010, 9:13am
 
This is how the code looks now.
the buttons code example helped to fix the grid , but i realize i still need to understand what i can do inside and outside of a class itself,

thankyou
Code:

//la grilla tiene cuadrados externos e internos,
//la diferencia de tamaño entre estos es de 2 veces el espesor de borde definido
//
int numrows = 8;
int numcols=8;
int rectWidthext = 50;
int rectHeightext = 50;
int rectborder=3;
int rectWidth=rectWidthext-2*rectborder;
int rectHeight=rectHeightext-2*rectborder;
// 2D Array of objects
Cell[][] grid;

void setup() {
 size(rectWidthext*numcols+2*rectborder,rectHeightext*numrows+2*rectborder);
 grid = new Cell[numcols][numrows];
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Initialize each object
     grid[i][j] = new Cell(i*50,j*50,50,50,i+j,0);
   }
 }

}

void draw() {
 background(0);
 fill(255);
 // The counter variables i and j are also the column and row numbers and
 // are used as arguments to the constructor for each object in the grid.  
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Oscillate and display each object
     grid[i][j].display();
     //grid[i][j].mouseover();
   }
 }
 
 //for (int y= 0; y<numrows; y ++){
  // for (int x= 0; x<numcols; x ++){
   //  int posx=rectborder+x*rectWidthext;//calcula la posicion del vertice en base al indice de la celda
   //  int posy=rectborder+y*rectHeightext;//idem

   //  rect(posx, posy,rectWidthext, rectHeightext);

    // if (mouseX > posx && mouseX < posx+rectWidthext && //verifica if mouse over
    // mouseY > posy && mouseY < posy+rectHeightext) {
     //  fill(0);//if over lo cambia de color
     //  rect(posx, posy,rectWidthext, rectHeightext);
       //incorporar aca el cambio de estado en una matriz
    // }
   //  fill(255);
   //}
// }

}

// A Cell object
class Cell {
 // A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
 float x,y;   // x,y location
 float w,h;   // width and height
 float angle; // angle for oscillating brightness
 int ccolor;//color de la celda para transmitir 0-255

 // Cell Constructor
 Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle,int tempcolor) {
   x = tempX;
   y = tempY;
   w = tempW;
   h = tempH;
   ccolor = tempcolor;
   angle = tempAngle;
   boolean over =false;
 }
 
 // Oscillation means increase angle
 void oscillate() {
   angle += 0.02;
 }

 void display() {
   stroke(255);
   // Color calculated using sine wave
   //fill(127+127*sin(angle));
   if (mouseover(x,y,w,h)) {
     ccolor=255;
   }
   else {ccolor =0 ;
   }
   fill(ccolor);
   rect(x+3,y+3,w,h);
   //print (x+" ");

 }
 
 //mouse over detection
 boolean mouseover(float x,float y, float w, float h) {
   
  if (pmouseX > x && pmouseX < x+w && //verifica if mouse over
     pmouseY > y && pmouseY < y+h) {
      return true;//if over lo cambia de color
     }
     return false;
 }
}

Re: Updating variables on Objects
Reply #6 - Jun 7th, 2010, 12:50am
 
At this point I wouldn't worry too much about whether you do something inside a class or not: mouse presses can easily be handled outside of the class.  However, you do need to identify how class methods can save you time.  In the code you just posted your mouseover method is inside the class so it already has access to class properties like x,y etc...  So there's no need to pass these as parameters.  This then makes it easier to call the mouseover() method from outside of the class.  Here's a quick example:

Code:
//la grilla tiene cuadrados externos e internos,
//la diferencia de tamaño entre estos es de 2 veces el espesor de borde definido
//
int numrows = 8;
int numcols=8;
int rectWidthext = 50;
int rectHeightext = 50;
int rectborder=3;
int rectWidth=rectWidthext-2*rectborder;
int rectHeight=rectHeightext-2*rectborder;
// 2D Array of objects
Cell[][] grid;

// Variable to store the currently 'active' cell
Cell activeCell;

void setup() {
 size(rectWidthext*numcols+2*rectborder,rectHeightext*numrows+2*rectborder);
 grid = new Cell[numcols][numrows];
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Initialize each object
     grid[i][j] = new Cell(i*50,j*50,50,50,i+j,0);
   }
 }

}

void draw() {
 background(0);
 fill(255);
 // The counter variables i and j are also the column and row numbers and
 // are used as arguments to the constructor for each object in the grid.  
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Oscillate and display each object
     grid[i][j].display();
     //grid[i][j].mouseover();
   }
 }
}

void mousePressed() {
  // With mousePressed outside of a class you just need to iterate over all relevant objects
  for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // this would be tedious to write if I had to pass all the parameters
     if(grid[i][j].mouseover()){
       activeCell = grid[i][j];
     }
   }
  }
}


// A Cell object
class Cell {
 // A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
 float x,y;   // x,y location
 float w,h;   // width and height
 float angle; // angle for oscillating brightness
 int ccolor;//color de la celda para transmitir 0-255

 // Cell Constructor
 Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle,int tempcolor) {
   x = tempX;
   y = tempY;
   w = tempW;
   h = tempH;
   ccolor = tempcolor;
   angle = tempAngle;
   boolean over =false;
 }
 
 // Oscillation means increase angle
 void oscillate() {
   angle += 0.02;
 }

 void display() {
   stroke(255);
   // Color calculated using sine wave
   //fill(127+127*sin(angle));
   if(activeCell == this) {
     ccolor = #ff9900;
   }
   else if (mouseover()) {
     ccolor=255;
   }
   else {ccolor =0 ;
   }
   fill(ccolor);
   rect(x+3,y+3,w,h);
   //print (x+" ");

 }
 
 //mouse over detection
 // No need to pass parameters:  Since this is a class method
 // it already has access to the class properties
 boolean mouseover() {
  // not sure why you were using pmouseX/Y - that's the mouse position on the previous frame    
  if (mouseX > x && mouseX < x+w && //verifica if mouse over
     mouseY > y && mouseY < y+h) {
      return true;//if over lo cambia de color
     }
     return false;
 }
}
Re: Updating variables on Objects
Reply #7 - Jun 7th, 2010, 7:52am
 
thankyou blindFish for the code fix,

Yes im still struggling on how to use objets to make the code easier more robust and reuse the objets on different parts.
The gray area to me is on how variables work on objetc´s, what needs to go on the main draw area on what on the objetc`s draw area, and an objet that makes use of another objet works.
like in the grid, mouse code, I rewrote a code to make use of your controlpoint object, but then I have to assign one control point per cell, so inside the Cell class I should be creating one control point ..
I tried different things, but is not working yet.., so i think this will be for latter improvement ..

I have a question if you don't mind,
my purpose on the code is to be able to latch or memorize the cell´s state, and once clicked it should toggle its state, at the same time each cell should have a color attribute stored .
should I add the following variables to the Cell object? :

cStatus : to memorize its on or off state
chighlight : to change its color if mouse over , or clicked without affecting its "stored" color .( probably wont need this )
ccolor  : to save the cell color , for future use, its the color shown if the status is on .

Thanks,
Re: Updating variables on Objects
Reply #8 - Jun 7th, 2010, 9:51am
 
In general anything specifically related to the object should happen within the class definition; so something like a state should be a class property rather than a global property...

The global 'activeCell' variable in my previous code is a good example to work with.  That's probably the best solution if there's only ever going to be a single active cell; but what if each cell can be 'active' as you want?  You could create a global array to contain active cells; but you'd be digging yourself a very big hole....   It's way simpler to give the Cell an 'active' property and change this when it is clicked:

Code:
//la grilla tiene cuadrados externos e internos,
//la diferencia de tamaño entre estos es de 2 veces el espesor de borde definido
//
int numrows = 8;
int numcols=8;
int rectWidthext = 50;
int rectHeightext = 50;
int rectborder=3;
int rectWidth=rectWidthext-2*rectborder;
int rectHeight=rectHeightext-2*rectborder;
// 2D Array of objects
Cell[][] grid;

void setup() {
 size(rectWidthext*numcols+2*rectborder,rectHeightext*numrows+2*rectborder);
 grid = new Cell[numcols][numrows];
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Initialize each object
     grid[i][j] = new Cell(i*50,j*50,50,50,i+j,0);
   }
 }

}

void draw() {
 background(0);
 fill(255);
 // The counter variables i and j are also the column and row numbers and
 // are used as arguments to the constructor for each object in the grid.  
 for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // Oscillate and display each object
     grid[i][j].display();
     //grid[i][j].mouseover();
   }
 }
}

void mousePressed() {
  // With mousePressed outside of a class you just need to iterate over all relevant objects
  for (int i = 0; i < numcols; i++) {
   for (int j = 0; j < numrows; j++) {
     // this would be tedious to write if I had to pass all the parameters
     if(grid[i][j].mouseover()){
       // toggle the active state
       grid[i][j].active = !grid[i][j].active;
     }
   }
  }
}


// A Cell object
class Cell {
 // A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
 float x,y;   // x,y location
 float w,h;   // width and height
 float angle; // angle for oscillating brightness
 int ccolor;//color de la celda para transmitir 0-255
 boolean active;

 // Cell Constructor
 Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle,int tempcolor) {
   x = tempX;
   y = tempY;
   w = tempW;
   h = tempH;
   ccolor = tempcolor;
   angle = tempAngle;
   boolean over =false;
 }
 
 // Oscillation means increase angle
 void oscillate() {
   angle += 0.02;
 }

 void display() {
   stroke(255);
   // Color calculated using sine wave
   //fill(127+127*sin(angle));
   // not too happy with duplicating the call to mouseover here...
   if(active && mouseover()){
     ccolor = #ffcc00;
   }
   else if (mouseover()) {
     ccolor=255;
   }
   else if(active) {
     ccolor = #ff9900;
   }
   else {
     ccolor =0 ;
   }
   fill(ccolor);
   rect(x+3,y+3,w,h);
   //print (x+" ");

 }
 
 //mouse over detection
 // No need to pass parameters:  Since this is a class method
 // it already has access to the class properties
 boolean mouseover() {
  // not sure why you were using pmouseX/Y - that's the mouse position on the previous frame    
  if (mouseX > x && mouseX < x+w && //verifica if mouse over
     mouseY > y && mouseY < y+h) {
      return true;
     }
     return false;
 }
}


Now when the mouse is pressed we toggle the value of the current object's active property... Simples!  <[sorry bad 'cultural' reference that will be lost on anyone outsise the UK].

Just to clarify this line:

grid[i][j].active = !grid[i][j].active;

This is a handy shortcut for toggling boolean variables.  It says "assign the variable to the opposite of itself".

Note: I wasn't too happy about making a repeated call to mouseover() when assiging the colour of the cell in the display() method.  If you find you use it a lot there's a good argument to make 'mouseover' a property and then just call the function once each frame to update it...
Re: Updating variables on Objects
Reply #9 - Jun 7th, 2010, 11:07am
 
Thanks a lot for your support,
I fully understand your explanations, and I now I think I understand how to use the class variables, my mistake was thinking that all variables should be declare as fields when declaring the class as the only way to relate that variable to the object instance, but I see this is not true, and I can use variables declared at the class level using the : Class.Variablename  , not needing to declare the variable as a field .

Gus

Re: Updating variables on Objects
Reply #10 - Jun 7th, 2010, 3:21pm
 
Hmm..  I'm not sure I follow you.  If you reference a property/field of a class with 'Class.VariableName' then 'VariableName' must have been declared at the class level.

So notice that I've added a declaration of "boolean active;" for the class; though I don't need to initialise that in the constructor with "active = false;" as 'false' is the default state for a boolean...

Also it's probably worth mentioning that in 'strict' OOP you would use getter and setter methods for accessing properties/fields rather than referencing them directly; but Processing is nice and lets us cheat Smiley

And TBH I'm not the best person to be explaining OOP: I tend to get confused when it comes to the terminology; though I'm pretty sure I'm using it correctly here... [actually I wasn't: I just corrected 'instantiate' - which refers to creating instances of objects - to 'initialise'; referring to the assignment of a value to a variable.]
Re: Updating variables on Objects
Reply #11 - Jun 8th, 2010, 12:28pm
 
ok, i understand,
I also need to be precise with the use of vocabulary when referring to the syntax,
my relation  with OOP is of "knowing" something superficially, not close to be friend of, and far from playing on the same team ...
Page Index Toggle Pages: 1