//******************************************************************** // BoxesPanel.java Author: Lewis/Loftus // // Demonstrates the use of conditionals and loops to guide drawing. //******************************************************************** import javax.swing.JPanel; import java.awt.*; import java.util.Random; public class BoxesPanel extends JPanel { private final int NUM_BOXES = 50, THICKNESS = 5, MAX_SIDE = 50; private final int MAX_X = 350, MAX_Y = 250; private Random generator; //----------------------------------------------------------------- // Sets up the drawing panel. //----------------------------------------------------------------- public BoxesPanel () { generator = new Random(); setBackground (Color.black); setPreferredSize (new Dimension(400, 300)); } //----------------------------------------------------------------- // Paints boxes of random size and size in a random location. // Narrow or short boxes are highlighted with a fill color. //----------------------------------------------------------------- public void paintComponent(Graphics page) { super.paintComponent (page); int x, y, size; for (int count = 0; count < NUM_BOXES; count++) { x = generator.nextInt(MAX_X) + 1; y = generator.nextInt(MAX_Y) + 1; size = generator.nextInt(MAX_SIDE) + 1; if (size <= THICKNESS) // check for narrow box { page.setColor (Color.yellow); page.fillOval (x, y, size, size); } else if (size <= THICKNESS) // check for short box { page.setColor (Color.green); page.fillOval (x, y, size, size); } else { page.setColor (Color.white); page.drawOval (x, y, size, size); } } } }