import java.awt.*; import java.util.*; import java.awt.event.*; public class Raindrops { static final int XSIZE = 600; static final int YSIZE = 600; static final int NDROPS = 50; static final int DEFAULTMAXHEIGHT = 8; static final int MAXRADIUS = 120; public int dropX[]; public int dropY[]; public int dropZ[]; public int dropR[]; public int dropStatus[]; int maxheight; Boolean clicked = Boolean.FALSE; public Raindrops() // constructor { dropX = new int[NDROPS]; dropY = new int[NDROPS]; dropZ = new int[NDROPS]; dropR = new int[NDROPS]; maxheight = DEFAULTMAXHEIGHT; nextTimeStep(); } public void nextTimeStep() { int i; for (i = 0; i < NDROPS; i++) { if (dropZ[i] == 0 && dropR[i] == 0) { // create a new raindrop dropX[i] = (int) (Math.random() * XSIZE); dropY[i] = (int) (Math.random() * YSIZE); dropZ[i] = (int) (Math.random() * maxheight); dropR[i] = 0; } else if (dropZ[i] > 0) { // let the raindrop fall a little dropZ[i] --; if (dropZ[i] == 0) { // it's hit the ground dropR[i] = 1;} } else if (dropR[i] > 0) { // let the splash spread dropR[i] +=6; if (dropR[i] >= MAXRADIUS) { // the splash has dissipated dropR[i] = 0; } } } } void resetDrops() { // mark each raindrop as uninitialized int i; for (i = 0; i < NDROPS; i++) { dropZ[i] = 0; dropR[i] = 0; } } public void setIndicator() { clicked = Boolean.TRUE; } public void handleKey(KeyEvent e) { int kc = e.getKeyCode(); switch (kc) { case KeyEvent.VK_R: resetDrops(); break; default: System.out.println("unknown keypress"); } } public void drawRain(Graphics2D graphics2D) { if (clicked) { graphics2D.setColor(Color.blue); graphics2D.fillRect(0, 10, XSIZE, 5);} int i, centerX, centerY, circleR, topLeftX, topLeftY, grayLightness; for (i = 0; i < NDROPS; i++) { if (dropR[i] > 0) { centerX = dropX[i]; centerY = dropY[i]; for (circleR = 0; circleR < dropR[i]; circleR += 10) { //graphics2D.setColor(Color.red); grayLightness = (int) (255 * (float) dropR[i] / MAXRADIUS); graphics2D.setColor(new Color(grayLightness, grayLightness, grayLightness)); topLeftX = centerX - circleR/2; topLeftY = centerY - circleR/2; graphics2D.drawOval(topLeftX, topLeftY, circleR, circleR); } } } } }