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 › null value in class...
Page Index Toggle Pages: 1
null value in class... (Read 540 times)
null value in class...
Jan 30th, 2010, 12:24pm
 
Hello there,  

This is my first time here so i would first like to say hi to everyone.
I'm very new with Processing but i find quite powerful and useful.

I'm having a problem with getting a value from a (very simple) custom object i made. It is instantiated inside the draw() method and each object is stored in an arrayList right after instantiation.
Basically i'm just trying to set a class property telling me if the object was clicked or not but when trying to set the property, from the method wasClicked(), i always get x and y coordinates valued to 0... Although the shapes appear on screen.

The final result will be a set of 3D boxes on which there will be a picture textured onto each side.

Here is the full code, any idea is welcome

Cheers

Terence

Code:

import processing.opengl.*;
import java.util.*;//ArrayList;

void setup()
{
 size(800, 800, OPENGL);
//  noStroke();
}

int i = 0;
float x =  70.0;
float y = 35.0;
float z = 120.0;
float Y = 0;
float Z = 0;

void mouseDragged() {
 if(mouseButton == LEFT) {
   x = pmouseX;
   y = pmouseY;    
 }
 if(mouseButton == RIGHT) {
   Y = mouseX/PI;
 }  
}

void mousePressed() {
 for(int cpt = 0; cpt < panels.size(); cpt++)
 {
   println(((panel)panels.get(i)).width);
//    panel tempPanel = (panel)panels.get(i);
//    if(tempPanel.wasClicked() == true) {
//      println("touché");
//    }
 }
}

void keyPressed() {
 if (key == CODED) {
   if (keyCode == RIGHT) {
     y = y + 10;
   }
   if (keyCode == LEFT) {    
     y = y - 10;    
   }    
   if (keyCode == UP) {
     Z = Z + 10;
   }
   if (keyCode == DOWN) {    
     Z = Z - 10;
   }
 }
}  

class panel extends PShape {
 public int xDeb, yDeb; // rectangle coordinates
 public boolean clicked;
 
 panel(int _xDeb, int _yDeb) {
   xDeb = _xDeb;
   yDeb = _yDeb;
   clicked = false;
   
   fill(255, 0, 0);
   stroke(4);
   //noStroke();
   rect(xDeb, yDeb, 50, 50);
 }
 
 int getX() { return this.xDeb; }
 int getY() { return this.yDeb; }
 
 boolean wasClicked() {  //xDeb and yDeb always have a value of 0
   println("getX: " + xDeb + " getY: " + this.getY() + " mouseX " + mouseX + " mouseY: " + mouseY); // ici, getX(), getY(), xDeb, this.xDeb, yDeb, this.yDeb sont tous à 0...
   
   if(this.getX() < mouseX && mouseX < (this.getX() + 50) && this.getY() < mouseY && mouseY < (this.getY() + 50)) {
     this.clicked = true;
   }
   return this.clicked;
 }
 
}



ArrayList panels = new ArrayList(); //arraylist containing the panel objects
//PImage a = loadImage("IMG_2725_tall.jpg");

void draw()
{
 panels.clear();
 background(255,255,255);
 
 pushMatrix();
 //camera(x, y, z, 00.0, 0.0, 0.0, 1.0, 0.0, 0.0);
 translate(x,y,Z);
 
 rotateY(Y);

 beginShape(LINES);  
 //axe des X
 fill(1,0,0); vertex(-400,0,0);
 fill(1,0,0); vertex(400,0,0);
 
 //axe des Y
 vertex(0,-400,0);
 vertex(0,400,0);

 //axe des Z
 vertex(0,0,-400);
 vertex(0,0,400);
 
 endShape();  
 
//  rotateY(radians(mouseX)/2);
//  rotateX(radians(mouseY)/2);

 
//  for(int j = 0; (j * 50)< (height - 50); i++)
//  {
//    for(int i = 0; (i * 50)< (width - 50); i++)
   for(int i = 0; (i * 50)< (100); i++)
   {
     int xDeb = i * 50;
     int xEnd = xDeb + 50;
   
     int yDeb = i * 50;
     int yEnd = yDeb + 50;
         
     beginShape(QUADS);
     //texture(a);
     
     //Face frontale
     vertex(xDeb,yDeb,0, 0, 0);
     vertex(xEnd,yDeb,0, 0, 489);
     vertex(xEnd,yEnd,0, 550, 489);
     vertex(xDeb,yEnd,0, 550, 0);
     
     //Face arriere
     vertex(xDeb,yDeb,50);
     vertex(xEnd,yDeb,50);
     vertex(xEnd,yEnd,50);  
     vertex(xDeb,yEnd,50);
     
     //face gauche
     vertex(xDeb,yEnd,0);
     vertex(xDeb,yDeb,0);
     vertex(xDeb,yDeb,50);
     vertex(xDeb,yEnd,50);
     
     //face droite
     vertex(xEnd,yEnd,0);
     vertex(xEnd,yDeb,0);
     vertex(xEnd,yDeb,50);
     vertex(xEnd,yEnd,50);
       
     //face dessous
     vertex(xDeb,yEnd,0);
     vertex(xDeb,yEnd,50);
     vertex(xEnd,yEnd,50);
     vertex(xEnd,yEnd,0);  
     
     //face dessus
     vertex(xDeb,yDeb,0);
     vertex(xDeb,yDeb,50);
     vertex(xEnd,yDeb,50);
     vertex(xEnd,yDeb,0);  
     
     endShape(CLOSE);        
 
     panel t = new panel(xDeb, yDeb); //new panel object
     panels.add(t);

   }
//  }
 popMatrix();
}
Re: null value in class...
Reply #1 - Jan 30th, 2010, 1:15pm
 
in your mousePressed method, your loop index is cpt. But in the same method, you call panels.get(i);
i is always zero here. do panels.get(cpt) et voila
Re: null value in class...
Reply #2 - Jan 31st, 2010, 2:18am
 
those little things... thank you very much !
Page Index Toggle Pages: 1