/* Complete description of state: 3 pods 9 plants per pod 3 bytes of input per plant: nearness,loudness,pitch 5 bytes of output per plant: r,g,b,height,angle */ import java.awt.*; public class PlantData extends BufferedApplet { int w = 0, h = 0; Graphics g; Font font = new Font("Helvetica", Font.PLAIN, 14); int mx = 0, my = 0; // ALL THE PARAMETERS final static int NEAR = 0; final static int LOUD = 1; final static int PITCH = 2; final static int RED = 3; final static int GREEN = 4; final static int BLUE = 5; final static int UP = 6; final static int TURN = 7; final static int NDATA = 8; // THE PARAMETER NAMES String name[] = {"near","loud","pitch","red","green","blue","up","turn"}; int nPlants = 27; // HOW MANY PLANTS THERE ARE // ALL THE PLANT DATA int data[][] = new int[nPlants][NDATA]; // BACKGROUND COLOR TO TINT THE MIDDLE THIRD OF THE APPLET WINDOW Color offWhite = new Color(240, 240, 240); public void render(Graphics g) { if (w == 0) { w = bounds().width; h = bounds().height; } // SET JUST THIS ONE DATA VALUE TO NON-ZERO, TO TRY THINGS OUT data[0][RED] = 255; this.g = g; g.setFont(font); g.setColor(Color.white); g.fillRect(0, 0, w, h); g.setColor(offWhite); if (scrim) g.fillRect(x(3)-DX/6, 0, x(6)-x(3), h); for (int i = 0 ; i < 27 ; i++) drawPlant(i); } // PARAMETERS CONTROLLING WHERE THINGS WILL DRAW ON THE SCREEN int X0 = 20; int Y0 = 30; int DX = 85; int DY = 170; int DD = 15; // CONVERT PLANT ID TO X,Y COORDINATES int x(int i) { return X0 + DX * (i % 9); } int y(int i) { return Y0 + DY * (i / 9); } // CONVERT X,Y COORDINATES TO PLANT IT int i(int x, int y) { int i = (y - Y0) / DY * 9 + (x - X0) / DX; return clip(i, 0, nPlants-1); } // CONVERT X,Y OOORDINATES TO DATA PARAMETER WITHIN THIS PLANT int d(int x, int y) { int d = ((y - Y0) % DY) / DD; return clip(d, 0, NDATA-1); } void drawPlant(int i) { int x = x(i); int y = y(i); for (int d = 0 ; d < NDATA ; d++) { y += DD; boolean selected = i == i(mx,my) && d == d(mx,my); // THE SELECTED ITEM PRINTS BLACK - ALL OTHERS PRINT BLUE g.setColor(selected ? Color.black : Color.blue); // DRAW SELECTED AGAIN, ONE PIXEL OVER, TO MAKE IT LOOK THICKER for (int r = 0 ; r < (selected ? 2 : 1) ; r++) { g.drawString(name[d], x+r, y); g.drawString("" + data[i][d], x+r + 45, y); } } } public boolean mouseMove(Event e, int x, int y) { mx = x; my = y; damage = true; return true; } public boolean keyUp(Event e, int key) { boolean shift = (e.modifiers & Event.SHIFT_MASK) > 0; switch (key) { case 1004: incr(shift ? 16 : 1); break; // ARROW UP - INCR VALUES case 1005: incr(shift ? -16 :-1); break; // ARROW DOWN - DECR VALUES case ' ': scrim = ! scrim; break; // SPACE KEY TO TOGGLE CENTER TINT } damage = true; return true; } void incr(int delta) { int i = i(mx, my); int d = d(mx, my); data[i][d] = clip(data[i][d] + delta, 0, 255); // DON'T ALLOW ILLEGAL VALUES } int clip(int n, int lo, int hi) { return Math.max(lo, Math.min(hi, n)); } boolean scrim = false; // INITIALLY, NO CENTER TINT }