I’m making a palette where when I press on a square it will change to whatever color I have selected from the palette. Right now I’m having trouble with two things:
- Making the array that when I select a circle or multiple circles, that entire row or column will fill with that color.
- Having a counter for the number of moves I make. I need some type of boolean or something so that whenever my mouse is pressed in the window of the grid the counter will go up by 1. I also need it to clear when I press the key ‘c’.
Here is my code so far, thank you for helping in advance:
//_______________________________________GRID array of class
int x=50, y=100, w=20, h=w, off=3, grid=8, many=grid*12; // adjust grid ( only if picture files available! )
Rect[] myGrid = new Rect[many]; // grid array
void set_grid() {
for (int i =0; i< myGrid.length; i++)
myGrid[i] = new Rect(x+(i%grid)*(w+off), y+(floor(i/grid))*(h+off), w, h, color(0, 0, 0));
}
void draw_grid() {
for (int i =0; i< myGrid.length; i++)
myGrid[i].display();
}
void clear() {
for (int i = 0; i < many; i++)
myGrid[i].c = color(0);
println("clear after key [c]");
}
class Rect {
color c;
float x, y, w, h;
Rect(float _x, float _y, float _w, float _h, color _c ) {
c = _c;
w = _w;
h = _h;
x = _x;
y = _y;
}
void display() {
push();
strokeWeight(1);
stroke(200, 200, 200);
if (over()) {
strokeWeight(3);
stroke(mainc);
}
fill(c);
rect(x, y, w, h);
pop();
sel();
}
boolean over() {
return(x < mouseX && mouseX < x+(w-off) && y < mouseY && mouseY < y+(h-off));
}
void sel() {
if ( over() )
if (mousePressed && mouseButton == LEFT)
c = mainc; //___________________ get color setpoint from main program
}
} // end class
//_________________ color select tool
color[] palette = {color(200, 200, 200), color(255, 0, 0), color(0, 255, 0), color(0, 0, 255), color(244, 245, 57), color(235, 57, 245), color(247, 22, 207), color(247, 195, 22)};
color mainc = color(200, 200, 200);
void drawPalette() {
int x0 = 50, x, y = 0, w = 30, h = 30, off = 40;
for (int i = 0; i < palette.length; i++) {
fill(palette[i]);
x = x0 + i*off;
rect(x, y, w, h);
if ((x < mouseX && mouseX < x+w && y < mouseY && mouseY < y+h) && mousePressed) mainc = palette[i];
}
}
void circleSelectors(){
noFill();
strokeWeight(1);
stroke(255);
//Row
int xCircle = 60, yCircle = 80, circleDiam = 15;
for (int i = 0; i < 8; i++)
ellipse(xCircle+i*23, yCircle, circleDiam, circleDiam);
//Columns
xCircle = 30;
yCircle = 110;
for (int i = 0; i < 12; i++)
ellipse(xCircle, yCircle+i*23, circleDiam, circleDiam);
}
void numMoves(){
fill(255);
textSize(18);
noStroke();
rect(150, height-80, 200, 30);
String word = "Num moves: ";
int x = 0;
String score = word + x;
fill(0);
text(score, 180, height-60);
}
//_______________________________________main
void setup() {
size(500, 500);
set_grid();
println("click color palette to select fill color\nclick rect to fill\nkey [c] to clear");
}
void draw() {
background(0);
drawPalette();
draw_grid();
circleSelectors();
numMoves();
}
void keyPressed() {
if (key == 'c')
clear();
}