Problem with my rectangle class
in
Programming Questions
•
6 months ago
Hello, I have a bug in my code, and I cant figure it out..
LedMatrixControl_Beta:
LedMatrixControl_Beta:
- myRect[][] box;
void setup() {
int i, j;
int L = 40;
size(800, 600);
box = new myRect[8][8];
for (i=0;i<8;++i) {
for (j=0;j<8;++j) {
box[i][j] = new myRect((i+1)*50, (j+1)*50, L, L);
}
}
//rect1.setOn();
}
void draw() {
background(100);
for (int i=0;i<8;++i) {
for (int j=0;j<8;++j) {
box[i][j].display();
}
}
}
void mousePressed() {
for (int i=0;i<8;++i) {
for (int j=0;j<8;++j) {
if(box[i][j].Click()){
box[i][j].setOn();
println(i+" "+j);
}
}
}
}
MyRect class:
- class myRect {
int x;
int y;
int w; // width
int h; // height
int state;
//constructor
myRect(int x_temp, int y_temp, int w_temp, int h_temp) {
x = x_temp;
y = y_temp;
w = w_temp;
h = h_temp;
state = 0; //off
}
int getState() {
return state;
}
void setOn() {
state = 1;
}
void setOff() {
state = 0;
}
boolean Click() {
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h)
return true;
return false;
}
void display() {
rect(x, y, w, h, 7);
if (state == 1) {
fill(149, 216, 245);
}
else {
fill(59, 116, 140);
}
}
}
This makes a rectangle matrix. The problem is if I click on a rectangle, then a wrong rectangle lights up..
Second problem:
(example).If I click at the third rectangle in the first row, the output is 2 0 instead of 0 2.
Can you help me?:)
Second problem:
(example).If I click at the third rectangle in the first row, the output is 2 0 instead of 0 2.
Can you help me?:)
1