Get Grid to generate new color per cell
in
Programming Questions
•
4 months ago
Hi guys, im having a bit of trouble with getting this grid to generate a new randomised colour for each cell on mousePressed. Ive got it working initally and i though that since the randomcolor() function is being called in setup, calling setup() again should provide a new set of randomised colors. Its not working so well.
Cell[][] grid; // 2D Array of objects
int cols = 30; // Number of columns and rows in the grid
int rows = 30;
void setup() {
size(300, 300);
grid = new Cell[cols][rows];
for (int i = 0; i < cols; i ++ ) {
for (int j = 0; j < rows; j ++ ) {
grid[i][j] = new Cell(i*10, j*10, 10, 10, i + j);
grid[i][j].display();
}
}
}
//void draw() {
//}
void keyPressed() {
for (int i = 0; i < cols; i ++ ) {
for (int j = 0; j < rows; j ++ ) {
grid[i][j].key();
setup();
}
}
}
And heres Cell Object
class Cell { // Cell object
float x, y; // x,y location
float w, h; // width and height
Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle) { // Cell Constructor
x = tempX;
y = tempY;
w = tempW;
h = tempH;
}
void randomcolor() { //Function to randomise colour
fill(random(255), random(255), random(255));
}
void display() {
stroke(0);
randomcolor();
rect(x, y, w, h);
}
void colchange() { //Function to randomise colour
randomcolor();
}
void key() {
colchange();
}
}
Cell[][] grid; // 2D Array of objects
int cols = 30; // Number of columns and rows in the grid
int rows = 30;
void setup() {
size(300, 300);
grid = new Cell[cols][rows];
for (int i = 0; i < cols; i ++ ) {
for (int j = 0; j < rows; j ++ ) {
grid[i][j] = new Cell(i*10, j*10, 10, 10, i + j);
grid[i][j].display();
}
}
}
//void draw() {
//}
void keyPressed() {
for (int i = 0; i < cols; i ++ ) {
for (int j = 0; j < rows; j ++ ) {
grid[i][j].key();
setup();
}
}
}
And heres Cell Object
class Cell { // Cell object
float x, y; // x,y location
float w, h; // width and height
Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle) { // Cell Constructor
x = tempX;
y = tempY;
w = tempW;
h = tempH;
}
void randomcolor() { //Function to randomise colour
fill(random(255), random(255), random(255));
}
void display() {
stroke(0);
randomcolor();
rect(x, y, w, h);
}
void colchange() { //Function to randomise colour
randomcolor();
}
void key() {
colchange();
}
}
1