// The "rain" applet import java.applet.*; import java.util.*; import java.awt.*; public class P6_22 extends Applet { private final int MAX_COUNT = 1000; private final int BUSY_WAIT = 90000000; private final int APPLET_SIZE = 500; private Random position = new Random(); private Raindrop[] rain = new Raindrop[5]; private Graphics page; public void init() { for (int drop = 0; drop < rain.length; drop++) rain[drop] = new Raindrop(); setSize (APPLET_SIZE, APPLET_SIZE); setVisible (true); page = getGraphics(); } public void start() { int count = 1; int wait; while (count < MAX_COUNT) { for (int drop = 0; drop < rain.length; drop++) check_drop (rain[drop]); count = count + 1; draw (page); wait = 0; while (wait < BUSY_WAIT) { wait = wait + 1; } } } public void check_drop (Raindrop drop) { if (drop.visible()) { drop.ripple(); } else { int x = Math.abs (position.nextInt() % APPLET_SIZE) + 1; int y = Math.abs (position.nextInt() % APPLET_SIZE) + 1; drop.set_position (x, y); } } public void draw (Graphics page) { page.setColor(getBackground()); page.fillRect (0, 0, APPLET_SIZE, APPLET_SIZE); page.setColor(getForeground()); for (int drop = 0; drop < rain.length; drop++) rain[drop].draw (page); } } class Raindrop { private final int MAX_RIPPLE = 30; private final int RIPPLE_STEP = 2; private static Random new_size = new Random(); private int current_size = 0; private int visible_size = 0; private int x = 1, y = 1; public boolean visible() { return current_size < visible_size; } public void set_position (int x_position, int y_position) { x = x_position; y = y_position; visible_size = Math.abs (new_size.nextInt() % MAX_RIPPLE) + 1; current_size = 1; } public void ripple() { x = x - RIPPLE_STEP/2; y = y - RIPPLE_STEP/2; current_size = current_size + RIPPLE_STEP; } public void draw (Graphics page) { page.drawOval (x, y, current_size, current_size); } }