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;
do {
n = in.readIntDialog("Enter the number of squares "
+ "(between 1 and " + MAXIMUM_NUMBER_OF_SQUARES + ")");
if (n <= 0 || n > MAXIMUM_NUMBER_OF_SQUARES) {
// display an error message
JOptionPane.showMessageDialog(null, "Invalid input",
"Input error", JOptionPane.WARNING_MESSAGE);
}
} while (n <= 0 || n > MAXIMUM_NUMBER_OF_SQUARES);
// Create the list of squares
ArrayList list = new ArrayList(n);
for (int i = 1; i <= n; i++) {
// random location (in the window)
int x = (int) ((WIDTH - 20) * Math.random());
int y = (int) ((HEIGHT - 20) * Math.random());
// random color among yellow, blue and red
Color c = null;
switch((int)(Math.random() * 3)) {
case 0:
c = Color.YELLOW;
break;
case 1:
c = Color.RED;
break;
case 2:
c = Color.BLUE;
break;
}
Rectangle r = new Rectangle(x,y,20,20,c,true);
list.add(r);
}
return list;
}
/**
* Erase the square at location (x,y) if any.
*/
public void eraseElementAt(int x, int y, ArrayList list) {
for (int i = list.size()-1; i >= 0; i --) {
Rectangle r = (Rectangle) list.get(i);
// Is (x,y) within r?
if (x >= r.getX() && x <= r.getX() + r.getWidth() &&
y >= r.getY() && y <= r.getY() + r.getHeight()) {
list.remove(i);
break; // exit the for loop
// here return would work as well
}
}
}
/**
* Display the number of red squares, blue squares and yellow squares
*/
public void displayStatistics(ArrayList list) {
// color counters
int blue = 0, red = 0, yellow = 0;
for (int i = 0; i < list.size(); i ++) {
// with generics, there is no need to cast
// to a Rectangle type
Rectangle r = list.get(i); //(Rectangle) list.get(i);
Color c = r.getColor();
if (c.equals(Color.RED)) {
red ++;
} else if (c.equals(Color.BLUE)) {
blue ++;
} else {
yellow ++;
}
}
String message = "Blue = " + blue +
"\nRed = " + red +
"\nYellow = " + yellow +
"\nTotal = " + list.size();
JOptionPane.showMessageDialog(null, message,
"Statistics", JOptionPane.INFORMATION_MESSAGE);
// 2nd approach: using an iterator
blue = red = yellow = 0;
Iterator it = list.iterator();
while(it.hasNext()) {
Rectangle r = it.next();
Color c = r.getColor();
if (c.equals(Color.RED)) {
red ++;
} else if (c.equals(Color.BLUE)) {
blue ++;
} else {
yellow ++;
}
}
message = "Blue = " + blue +
"\nRed = " + red +
"\nYellow = " + yellow +
"\nTotal = " + list.size();
JOptionPane.showMessageDialog(null, message,
"Statistics", JOptionPane.INFORMATION_MESSAGE);
// using a for-each loop
blue = red = yellow = 0;
for (Rectangle r: list) {
Color c = r.getColor();
if (c.equals(Color.RED)) {
red ++;
} else if (c.equals(Color.BLUE)) {
blue ++;
} else {
yellow ++;
}
}
message = "Blue = " + blue +
"\nRed = " + red +
"\nYellow = " + yellow +
"\nTotal = " + list.size();
JOptionPane.showMessageDialog(null, message,
"Statistics", JOptionPane.INFORMATION_MESSAGE);
}
}