Hello,
very new to processing and only little programming experience here. I'm trying to have a 2d grid with cells that change colour once I click them. My code basically works, but the execution seems to be very glitchy. Sometimes I need to click my mouse several times until a gridcell finally constantly changes its colour. Does anyone a more reliable coding solution for this?
Ideally I should also be able to hold down the mousebutton and draw, but this doesn't work at all here.
very new to processing and only little programming experience here. I'm trying to have a 2d grid with cells that change colour once I click them. My code basically works, but the execution seems to be very glitchy. Sometimes I need to click my mouse several times until a gridcell finally constantly changes its colour. Does anyone a more reliable coding solution for this?
Ideally I should also be able to hold down the mousebutton and draw, but this doesn't work at all here.
- Cell[][] grid;
int cols = 20;
int rows = 20;
int cellsize = 20;
void setup() {
size(rows*cellsize,cols*cellsize);
grid = new Cell[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// Initialize each object
grid[i][j] = new Cell(i*cellsize,j*cellsize,cellsize,cellsize, true, false);
}
}
}
void draw() {
background(0);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
grid[i][j].overcell();
grid[i][j].pressed();
grid[i][j].display();
}
}
}
//void update() {
//if Cell.rollover
class Cell {
float x,y; // x,y location
float w,h; // width and height
boolean p; //walkable?
boolean over;
// a constructor which gives initial values once class is called!
Cell(float tempX, float tempY, float tempW, float tempH, boolean tempP, boolean tempOver) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
p = tempP;
over = tempOver;
}
void pressed() {
if(over && mousePressed) {
p = !p;
}
}
void overcell() {
if (rollover() == true) {
over = true;
} else {
over = false;
}
}
boolean rollover() {
if(mouseX >= x && mouseX <= x + w && mouseY >= y && mouseY <= y + h) {
return true;
} else {
return false;
}
}
void display() {
stroke(255);
// Color calculated using sine wave
if (p == true){fill(127);} else {fill(0);};
rect(x,y,w,h);
}
}
1