How to change the colour of an objec in an 2D arrayt permanently?
in
Programming Questions
•
4 months ago
I have created a maze of grids using a 2D array and I'm trying to change the colour of a grid by right clicking or left clicking on a grid in order to indicate the start/end position for the maze by making that grid blue.
Right now my coding allows me to change the colour of a grid when I pressed down the moue button, however it turned back to its original colour (black/white) when i released it. How do I make it stays blue?? Thanks.
Here's my coding:
Maze[][] m; //2D array
int col = 25; //numbers of rows
int row = 25; //numbers of columns
char status;
void setup() {
size (500, 500);
m = new Maze[col][row];
for (int i=0; i<col; i++) { //Initiating each object in the maze
for (int j=0; j<row; j++) {
m[i][j] = new Maze(i*20, j*20, random(1.0), 20, 20, "status");
}
}
}
void draw() {
for (int i=0; i<col; i++) { //i= # of columns
for (int j=0; j<row; j++) { //j = # of rows
if (m[i][j].r>0.3) //70% of assigning white rectangle to grids
m[i][j].drawWhite(); else //30% chance of assigning black rectangle to grids
m[i][j].drawBlack();
if (mousePressed) {
switch (mouseButton) {
case LEFT:
if (mouseX > m[i][j].x && mouseX < m[i][j].x + m[i][j].w && mouseY > m[i][j].y && mouseY < m[i][j].y + m[i][j].h) {
m[i][j].status = "start";
m[i][j].drawBlue();
}
break;
case RIGHT:
if (mouseX > m[i][j].x && mouseX < m[i][j].x + m[i][j].w && mouseY > m[i][j].y && mouseY < m[i][j].y + m[i][j].h) {
m[i][j].status = "end";
m[i][j].drawBlue();
break;
}
}
}
}
}
}
class Maze {
int x, y, w, h; // x,y location, width and height
float r; //colour determing index
String status;
Maze (int p1, int p2, float p3, int p4, int p5, String p6) {
x = p1; //grid's x-coordinate
y = p2; //grid's y-coordinate
r = p3; //grid's colour determining index
w = p4; //grid's width
h = p5; //grid's height
status = p6;
}
void drawWhite() { //draw a white rect in a grid
fill (255, 255, 255);
rect (x, y, w, h);
}
void drawBlack() { //draw a black rect in a grid
fill (0);
rect (x, y, w, h);
}
void drawBlue() {
fill(0, 0, 255);
rect (x, y, w, h);
}
}
1