/* built with Studio Sketchpad: * https://sketchpad.cc * * observe the evolution of this sketch: * https://studio.sketchpad.cc/sp/pad/view/ro.qHekrawcA4r/rev.13 * * authors: * GoToLoop * license (unless otherwise specified): * creative commons attribution-share alike 3.0 license. * https://creativecommons.org/licenses/by-sa/3.0/ */ /** * Rectangle Toggle (v2.35) * by szaboa1 (2013/Apr) * mod GoToLoop * * 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.9ABG0RKl9D2Bx/latest */ final static byte GRID = 8, MARGIN = 25; final static byte DIM = 60, GAP = 15, LEN = DIM + GAP; final static Square[][] boxes = new Square[GRID][GRID]; 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(0100); 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); } void draw() { for (int row=0; row!=GRID; ++row) for (int col=0; col!=GRID; ++col) boxes[row][col].display(); } void mousePressed() { for (int row=0; row!=GRID; ++row) for (int col=0; col!=GRID; ++col) if ( boxes[row][col].click() ) { println( col + "," + row + " : " + boxes[row][col].toggle() ); redraw(); return; } } final class Square { final short x, y, w, h; boolean state; final static color COLOR_ON = #90D0F0, COLOR_OFF = #407090; Square(int xx, int yy, int ww, int hh) { x = (short) xx; y = (short) yy; w = (short) ww; h = (short) hh; } boolean toggle() { return state = !state; } boolean click() { return mouseX > x & mouseX < x+w & mouseY > y & mouseY < y+h; } void display() { fill(state? COLOR_ON:COLOR_OFF); rect(x, y, w, h); } }