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
* (the graphics elements are squares)
*/
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;
/**
* 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
Input in = new Input();
int n;
boolean badInput;
do {
n = in.readIntDialog("Number of squares (between 1 and "
+ MAXIMUM_NUMBER_OF_SQUARES + ")?");
badInput = n < 1 || n > MAXIMUM_NUMBER_OF_SQUARES;
if (badInput) {
// display an error message
JOptionPane.showMessageDialog(null,
"Invalid number of squares", "Input error",
JOptionPane.WARNING_MESSAGE);
}
} while (badInput);
// Create the list of squares
ArrayList list = new ArrayList(n);
for (int i = 1; i <= n; i++) {
// Create a square
// location: random in the window
int x = (int) (Math.random() * (WIDTH - 20));
int y = (int) (Math.random() * (HEIGHT - 20));
// color: randomly chosen among blue, red and yellow
Color c = null;
switch ((int) (Math.random() * 3)) {
case 0:
c = Color.RED;
break;
case 1:
c = Color.BLUE;
break;
case 2:
c = Color.YELLOW;
break;
}
Rectangle s = new Rectangle(x, y, 20, 20, c, true);
list.add(s);
}
return list;
}
/**
* 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 s = (Rectangle) listOfSquares.get(i);
if (x >= s.getX() && x <= s.getX() + s.getWidth() && y >= s.getY()
&& y <= s.getY() + s.getHeight()) {
listOfSquares.remove(i);
break; // breaks out of the for loop
}
}
}
/**
* Display the number of red squares, blue squares and yellow squares
*/
public void displayStatistics(ArrayList listOfSquares) {
int red = 0, blue = 0, yellow = 0;
for (int i = 0; i < listOfSquares.size(); i++) {
Rectangle s = (Rectangle) listOfSquares.get(i);
if (s.getColor().equals(Color.RED)) {
red++;
} else if (s.getColor().equals(Color.BLUE)) {
blue++;
} else {
yellow++;
}
}
String message = "Yellow squares = " + yellow + "\nBlue squares = "
+ blue + "\nRed squares = " + red + "\nTotal squares = "
+ listOfSquares.size();
JOptionPane.showMessageDialog(null, message, "Statistics",
JOptionPane.INFORMATION_MESSAGE);
// Use a for each loop
red = yellow = blue = 0;
for (Object obj : listOfSquares) {
Rectangle s = (Rectangle) obj;
if (s.getColor().equals(Color.RED)) {
red++;
} else if (s.getColor().equals(Color.BLUE)) {
blue++;
} else {
yellow++;
}
}
message = "Yellow squares = " + yellow + "\nBlue squares = " + blue
+ "\nRed squares = " + red + "\nTotal squares = "
+ listOfSquares.size();
JOptionPane.showMessageDialog(null, message, "Statistics",
JOptionPane.INFORMATION_MESSAGE);
// Use an iterator
red = yellow = blue = 0;
Iterator it = listOfSquares.iterator();
while (it.hasNext()) {
Rectangle s = (Rectangle) it.next();
if (s.getColor().equals(Color.RED)) {
red++;
} else if (s.getColor().equals(Color.BLUE)) {
blue++;
} else {
yellow++;
}
}
message = "Yellow squares = " + yellow + "\nBlue squares = " + blue
+ "\nRed squares = " + red + "\nTotal squares = "
+ listOfSquares.size();
JOptionPane.showMessageDialog(null, message, "Statistics",
JOptionPane.INFORMATION_MESSAGE);
}
}