import java.awt.Color; import java.util.*; import uwcse.io.*; import uwcse.graphics.*; import javax.swing.*; /** * A class to create and manipulate graphics elements stored in an ArrayList */ public class GraphicsElements { /** Maximum number of disks in a pile of disks */ public static final int MAXIMUM_NUMBER_OF_SQUARES = 100; /** Width of the window (from ViewWindow) */ public static final int WIDTH = ViewWindow.WINDOW_WIDTH; /** Height of the window (from ViewWindow) */ public static final int HEIGHT = ViewWindow.WINDOW_HEIGHT; // Put your other instance fields here (if you need any) /** * Generate a list of squares randomly located in the window. Use filled * squares. The color of each square is either yellow, blue or red.
* Store the squares in an ArrayList and return that ArrayList
* The number of squares is given by the user (use a dialog box). If that * number is less than or equal to 0 or greater than * MAXIMUM_NUMBER_OF_SQUARES, display an error message (use * JOptionPane.showMessageDialog)and ask for it again. */ public ArrayList getRandomSquares() { // Get the number of squares from the user int numberOfSquares; Input input = new Input(); boolean askAgain; do { numberOfSquares = input .readIntDialog("Number of squares (between 1 and " + GraphicsElements.MAXIMUM_NUMBER_OF_SQUARES + ")"); if (numberOfSquares <= 0 || numberOfSquares > GraphicsElements.MAXIMUM_NUMBER_OF_SQUARES) { JOptionPane.showMessageDialog(null, "Invalid value", "Error", JOptionPane.ERROR_MESSAGE); askAgain = true; } else askAgain = false; } while (askAgain); // Create the list of squares ArrayList listOfSquares = new ArrayList(); int size = 20; for (int i = 0; i < numberOfSquares; i++) { // location int x = (int) (Math.random() * (GraphicsElements.WIDTH - size)); int y = (int) (Math.random() * (GraphicsElements.HEIGHT - size)); // color (randomly chosen between red, blue and yellow) Color c = null; switch ((int) (Math.random() * 3)) { case 0: c = Color.blue; break; case 1: c = Color.red; break; case 2: c = Color.yellow; break; } // the square Rectangle r = new Rectangle(x, y, size, size, c, true); listOfSquares.add(r); } return listOfSquares; } /** * Erase the square at location (x,y) if any. */ public void eraseElementAt(int x, int y, ArrayList listOfSquares) { for (int i = listOfSquares.size() - 1; i >= 0; i--) { Rectangle r = (Rectangle) listOfSquares.get(i); if (x >= r.getX() && x <= r.getX() + r.getWidth() && y >= r.getY() && y <= r.getY() + r.getHeight()) { listOfSquares.remove(i); return; } } } /** * Display the number of red squares, blue squares and yellow squares */ public void displayStatistics(ArrayList listOfSquares) { int redNum = 0; int blueNum = 0; int yellowNum = 0; Iterator it = listOfSquares.iterator(); while (it.hasNext()) { Rectangle r = (Rectangle) it.next(); if (r.getColor().equals(Color.blue)) blueNum++; else if (r.getColor().equals(Color.red)) redNum++; else if (r.getColor().equals(Color.yellow)) yellowNum++; } String stats = "Total number of squares = " + listOfSquares.size() + "\n" + "red = " + redNum + "\n" + "blue = " + blueNum + "\n" + "yellow = " + yellowNum; JOptionPane.showMessageDialog(null, stats, "statistics", JOptionPane.INFORMATION_MESSAGE); } }