/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.DphPsN1XAl2/rev.10
*
* authors:
* GoToLoop
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/**
* Rectangle Toggle 3 (v3.31)
* by szaboa1 (2013/Jun)
* mod GoToLoop
* for PillowPenguin
*
* http://forum.processing.org/topic/problem-with-my-rectangle-class
*
* http://forum.processing.org/topic/draggable-balls-to-any-cell
*
* http://forum.processing.org/topic
* /how-to-change-the-colour-of-an-objec-in-an-2d-arrayt-permanently
*
* http://studio.processingtogether.com/sp/pad/export/ro.93MhRWK35nIP8/latest
*/
final static byte GRID = 25, MARGIN = 10;
final static byte DIM = 30, GAP = 0, LEN = DIM + GAP;
final static Square[][] boxes = new Square[GRID][GRID];
final static color BORDER = 0, BG = 0100;
final static float BOLD = 2.5;
final static String GFX = JAVA2D; // Use JAVA2D or P2D
void setup() {
size(GRID*LEN + MARGIN*2 - GAP, GRID*LEN + MARGIN*2 - GAP, GFX);
noLoop();
background(BG);
for (int row=0; row!=GRID; ++row) for (int col=0; col!=GRID; ++col)
boxes[row][col] = new Square(col*LEN + MARGIN, row*LEN + MARGIN,
DIM, DIM, random(1) < Square.CHANCE? Square.COLOR_1:Square.COLOR_0);
}
void draw() {
for (int row=0; row!=GRID; ++row) for (int col=0; col!=GRID; ++col)
boxes[row][col].display();
}
void keyPressed() {
for (int row=0; row!=GRID; ++row) for (int col=0; col!=GRID; ++col)
boxes[row][col].redo();
redraw();
}
void mousePressed() {
final int btn = mouseButton;
final color chosenColor;
if (btn == LEFT) chosenColor = Square.COLOR_1;
else if (btn == RIGHT) chosenColor = Square.COLOR_2;
else chosenColor = Square.COLOR_3;
for (int row=0; row!=GRID; ++row) for (int col=0; col!=GRID; ++col)
if ( boxes[row][col].click() ) {
boxes[row][col].setOrTurnOff(chosenColor);
redraw();
return;
}
}
final class Square {
final static color COLOR_0 = 0200, COLOR_1 = #90D0F0;
final static color COLOR_2 = #FF0000, COLOR_3 = #00FF00;
final static float CHANCE = .3;
final short x, y, w, h, xw, yh;
color c; // current active color.
Square(int xx, int yy, int ww, int hh) {
this(xx, yy, ww, hh, COLOR_0);
}
Square(int xx, int yy, int ww, int hh, color cc) {
x = (short) xx;
y = (short) yy;
w = (short) ww;
h = (short) hh;
xw = (short) (x+w);
yh = (short) (y+h);
c = cc;
}
color redo() {
return c = random(1) < CHANCE? COLOR_1:COLOR_0;
}
color setOrTurnOff(color cc) {
return c = c == COLOR_0? cc:COLOR_0;
}
boolean click() {
return mouseX > x & mouseX < xw & mouseY > y & mouseY < yh;
}
void display() {
fill(c);
rect(x, y, w, h);
}
}