1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| class LED { int x; int y; int state; int k; int w; int degs; int c; LED(int x_, int y_, int w_) { x = x_; y = y_; w = w_; c = (int)random(0,360); state = 0; k = 0; degs = 0; } void update() { if (overRect(x-w/2, y-w/2, w, w)) { state ++; if (state >= 3) { state = 0; } } } void draw() { colorMode(HSB,360,100,100); if (state == 0) { pushMatrix(); translate(x, y); rotate(radians(degs)); fill(c,100,100); noStroke(); rectMode(CENTER); rect(0, 0, w, w); popMatrix(); } else if (state == 1) { pushMatrix(); translate(x, y); rotate(radians(++degs)); fill(c,100,100); noStroke(); rectMode(CENTER); rect(0, 0, w, w); popMatrix(); } else if (state == 2) { pushMatrix(); translate(x, y); rotate(radians(degs)); if (k == 0) c ++; if (k == 1) c --; if (c <=0) { c = 0; k = 0; } if (c >= 360) { c = 360; k = 1; } fill(c,100,100); noStroke(); rectMode(CENTER); rect(0, 0, w, w); popMatrix(); } } boolean overRect(int x, int y, int width, int height) { if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) { return true; } else { return false; } } }
|