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 › Matrix interactive HELP!!!
Page Index Toggle Pages: 1
Matrix interactive HELP!!! (Read 276 times)
Matrix interactive HELP!!!
Nov 21st, 2008, 5:18pm
 
Hello,

i'm trying to make a sketch with a matrix of 400 squares(20,20) and when the mouse rollover one square, it ligths up.
i started the skecth from Daniel Shiffman exmaple 9-10,
all "mouse rollover" part is working but i can't make the matrix and i can't see what's the problem ??
in general for making the matrix i'll do something like this:

 for (int i = 0; i < 20; i++ ) {
  for (int i2 = 0; i2 < 20; i2++) {  
   rect(i*10, i2*10, 10, 10);
 }
}


AND THIS IS MY SKETCH:




Square [] squares = new Square[20];

void setup() {
 size(200,200);
 
 // Initialize all Square objects
 for (int i = 0; i < squares.length; i++ ) {
  for (int i2 = 0; i2 < squares.length; i2++){  
   squares[i] = new Square(i*10, i2*10);
 }
}
}

void draw() {
 
 background(100);
 // Move and display all Square objects
 for (int i = 0; i < squares.length; i ++ ) {
   // Check if mouse is over the Square
   squares[i].rollover(mouseX,mouseY); // Passing the mouse coordinates into an object.
   //stripes[i].move();
   squares[i].display();
 }
}


class Square {
 
 float x;     // horizontal location of Square
 float y;  
 float w;     // width of stripe
 
 
 boolean mouse;

 Square(float x2, float y2) {
   // All stripes start at 0
   x = x2;
   y = y2;

   w = 10;
   mouse = false;
 }

 // Draw square
 void display() {
 
   // Boolean variable determines Square color.
   if (mouse) {
     fill(255);
   } else {
     fill(255,100);
   }
   
   noStroke();
   rect(x, y, w, w);
 }


// Check to see if point (mx,my) is inside the Square.
 void rollover(int mx, int my) {
   // Left edge is x, Right edge is x + w
   if (mx > x && mx < x + w) {
     mouse = true;
   } else {
     mouse = false;
   }
 }
}






Sorry my english, thanks for your help.
Re: Matrix interactive HELP!!!
Reply #1 - Nov 21st, 2008, 5:39pm
 
It works for me: I see a light band at the bottom of the sketch and the square at the x coordinates of the mouse is lighter.

Currently you generate only a band: 20 squares instead of 400. And you test only the x coordinate, not x and y (spacial comparison).
Re: Matrix interactive HELP!!!
Reply #2 - Nov 21st, 2008, 7:30pm
 
thanks philho!

i'm testing "y" now but everytime i'm trying to generate more than 20 squares, i can't see it.
if i generate 1, the line is in the top,
if i generate 5, the line is in the middle
and if i generate 20, the line is at the end.

why processing is only keeping the last line of code ?


here is my new code:


// An array of square
Square[] squares = new Square[10];

void setup() {
 size(200,200);
 
 // Initialize all square objects
 for (int i = 0; i < squares.length; i++ ) {
  for (int i2 = 0; i2 < squares.length; i2++){  
   squares[i] = new Square(i*10, i2*10);
 }
}
}

void draw() {
 
 background(100);
 // Move and display all square objects
 for (int i = 0; i < squares.length; i ++ ) {
   // Check if mouse is over the square
   squares[i].rollover(mouseX,mouseY); // Passing the mouse coordinates into an object.
   //square[i].move();
   squares[i].display();
 }
}



class Square {
 
 float x;  // horizontal location of square
 float y;  
 //float speed; // speed of square
 float w;     // width of square
 
 // A boolean variable keeps track of the object's state.
 boolean mouse; // state of square (mouse is over or not?)

 Square(float x2, float y2) {
   // All squares start at 0
   x = x2;
   y = y2;
   // All squares have a random positive speed
   //speed = random(1);
   w = 10;
   mouse = false;
 }

 // Draw stripe
 void display() {
   
   // Boolean variable determines square color.
   if (mouse) {
     fill(255);
   } else {
     fill(255,100);
   }
   
   noStroke();
   rectMode(CENTER);
   rect(x+w/2, y+w/2, w, w);
 }

/* // Move square
 void move() {
   x += speed;
   if (x > width + 20) x = -20;
 } */

// Check to see if point (mx,my) is inside the square.
 void rollover(int mx, int my) {
   // Left edge is x, Right edge is x + w
   if (mx > x && mx < x + w && my > y && my < y + w  ) {
     mouse = true;
   } else {
     mouse = false;
   }
   
 }
}
Re: Matrix interactive HELP!!!
Reply #3 - Nov 21st, 2008, 10:36pm
 
Mmm, it is quite worse, you reduced the number of squares instead of increasing it... I see a number of issues with your code, but it is OK for learning purpose, I suppose.
Here are my changes:
Code:
int ARRAY_SIZE = 20;
// An array of squares
Square[] squares = new Square[ARRAY_SIZE * ARRAY_SIZE];

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

// Initialize all square objects
for (int r = 0; r < ARRAY_SIZE; r++ ) {
for (int c = 0; c < ARRAY_SIZE; c++){
squares[r * ARRAY_SIZE + c] = new Square(r*10, c*10);
}
}
}
// ...
// Simplified routine
void rollover(int mx, int my) {
// Left edge is x, Right edge is x + w
mouse = mx > x && mx < x + w && my > y && my < y + w;
}

but actually you can do the same effect without classes nor memory use...
Code:
int ARRAY_SIZE = 20;
int sideSize;

void setup() {
size(200, 200);
sideSize = width / ARRAY_SIZE;
}

void draw() {
background(100);
int posX = mouseX / sideSize;
int posY = mouseY / sideSize;
fill(255);
noStroke();
rect(posX * sideSize, posY * sideSize, width / ARRAY_SIZE, height / ARRAY_SIZE);
}

But playing with classes is good, too, and might allow to do more flexible/complex things.
Re: Matrix interactive HELP!!!
Reply #4 - Nov 22nd, 2008, 6:59pm
 
WOw!! thanks a lot!!! Cheesy

I'm using this skecth only for learning porpuses, to work with classes, and i use squares beacuse is easy... Smiley

I have only more question...

when you use : (to initialize all square objects)

...
squares[r * ARRAY_SIZE + c] = new Square(r*10, c*10)
...


what i think is happening in the "draw" with two "for":

squares[0 * 20 + 0]  /// = 0

squares[0 * 20 + 1] /// =  1

squares[0 * 20 + 2] /// =  2

etc...


squares[1 * 20 + 0] /// = 20

squares[1 * 20 + 1] /// = 21

squares[1 * 20 + 2] /// = 22

etc...

that's rigth?



and thank you for the simplified routine of "mouse", is very usefull.... Cheesy
with your technic, now, i don't need to specify whether is true or false, with "if" condition.

tHANKS AGAIN!!!!!




Page Index Toggle Pages: 1