import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import m865.shapepack.Shape;
import m865.shapepack.Rectangle;
import m865.shapepack.Ellipse;
import m865.shapepack.Square;
/**
* This applet paints a rectangle, ellipse or square of the color you've chosen
* wherever you click. This applet keeps a list of the shapes you've drawn
* and paints all the shapes in the list when it repaints.
*
* Java's classes:
*
* - Applet (applet)
*
- Event (awt) user-generated action
*
- Graphics (awt) used for drawing
*
- Color (awt) defines colors
*
- Choice (awt) shape and color selection choices
*
- Vector (util) list of shapes
*
*
* Custom classes:
*
* - SimpleDraw1 the applet/application
*
- Shape the common ancestor for all shapes
*
- Rectangle defines and draws rectangles
*
- Ellipse defines and draws ellipses
*
- Square defines and draws squares
*
* @author Daniel D. Warner
*
* @version 1.0 09/01/05
*
*/
public class SimpleDraw1 extends Applet {
/**
* default Width for new shapes
*/
public static final int defaultWidth = 150;
/**
* default Height for new shapes
*/
public static final int defaultHeight = 75;
/**
* The list of shapes to be drawn
*/
Vector drawnShapes;
/**
* The shape selector
*/
Choice shapeChoice;
/**
* The color selector
*/
Choice colorChoice;
/**
* The button for clearing the list of shapes
*/
Button clearButton;
/**
* Initializes the Graphical User Interface.
*/
public void init() {
drawnShapes = new Vector();
shapeChoice = new Choice();
shapeChoice.addItem("Rectangle");
shapeChoice.addItem("Ellipse");
shapeChoice.addItem("Square");
add(shapeChoice);
colorChoice = new Choice();
colorChoice.addItem("Red");
colorChoice.addItem("Green");
colorChoice.addItem("Blue");
add(colorChoice);
clearButton = new Button("Clear");
clearButton.setBackground(Color.white);
add(clearButton);
}
/**
* Draw all the shapes.
*/
public void paint(Graphics g) {
Shape s;
int numShapes;
numShapes = drawnShapes.size();
for (int i = 0; i < numShapes; i++) {
s = (Shape)drawnShapes.elementAt(i);
// When the shape draws, rectangles, ellipsess and squares each invoke
// their own draw method, depending on which shape this is.
s.draw(g);
}
}
/**
* When the user clicks the mouse in the drawing area, create a new shape
* at the location of the mouse.
*/
public boolean mouseUp(Event e, int x, int y) {
Shape s; // This concrete shape will be specified by the shape selector.
Color c;
// Send action info to the console.
System.out.println("MouseUp at (" + x + ", " + y + ")");
String shapeString = shapeChoice.getSelectedItem();
String colorString = colorChoice.getSelectedItem();
if (colorString.equals("Red"))
c = Color.red;
else if (colorString.equals("Green"))
c = Color.green;
else
c = Color.blue;
if (shapeString.equals("Rectangle"))
s = new Rectangle(x, y, defaultWidth, defaultHeight, c);
else if (shapeString.equals("Ellipse"))
s = new Ellipse(x, y, defaultWidth, defaultHeight, c);
else
s = new Square(x, y, defaultHeight, c);
outputStuff( s );
drawnShapes.addElement(s);
repaint();
return true;
}
/**
* Clears all the shapes when the clear button is clicked.
*/
public boolean action(Event e, Object arg) {
System.out.println("Action Event");
if (e.target == clearButton) {
drawnShapes.removeAllElements();
}
repaint();
return true;
}
/**
* Print shape specific information to the console.
*
* @param x the specific shape to be examined
*/
public static void outputStuff( Shape x ) {
System.out.println( x );
System.out.println( "Area: " + x.area());
System.out.println( "Perimeter: " + x.perimeter());
System.out.println( );
}
// This applet can also run as a stand alone application
public static void main(String args[]) {
Frame f = new Frame();
Applet a = new SimpleDraw1();
f.add(a, "Center");
a.init();
f.setSize(400,400);
f.show();
f.addWindowListener(
new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
}