/* This applet demonstrates the minimum functionality required by assignment J1. Author: Jeremy Baer */ // import some packages we will need import java.awt.*; import java.awt.event.*; import java.util.*; import java.applet.Applet; public class J1Applet extends Applet implements ActionListener { // a couple of constants to help keep track of what type of object we are going to draw private static final int SQUARE = 0; private static final int CIRCLE = 1; // two instance variables to hold the squares and circles buttons private Button squares, circles; // a variable we use to hold the current type of object to create private int selectedTool = SQUARE; // instance of the inner class we define below private drawingArea myCanvas; // list of the objects that the user creates private Vector objectList = new Vector(10,10); ///////////////////////////////////////////////////////////////// // init ///////////////////////////////////////////////////////////////// /** * Create the square and circle buttons and lay them out. Also * create a drawing area and add it to the applet's layout. * * @return None */////////////////////////////////////////////////////////////// public void init() { // set the layout manager of the applet setLayout(new BorderLayout()); // create a panel to hold the buttons and set its layout manager to a flow layout Panel buttonPanel = new Panel(); buttonPanel.setLayout(new FlowLayout()); // create the "Red" button, setup the action listener for the button // (this applet), and add it to the button panel squares = new Button("Squares"); squares.addActionListener(this); buttonPanel.add(squares); // create the "Blue" button, setup the action listener for the button // (this applet), and add it to the button panel circles = new Button("Circles"); circles.addActionListener(this); buttonPanel.add(circles); // create the drawing area and add it myCanvas = new drawingArea(); this.add("Center", myCanvas); // add the panel containting the buttons to the bottom of the applet. this.add("South", buttonPanel); } ///////////////////////////////////////////////////////////////// // actionPerformed ///////////////////////////////////////////////////////////////// /** * This method is declared abstractly in the ActionListener * interface, and is required in order for the applet to * implement that interface. It is used here to handle button * presses on the "Square" and "Circle" buttons * * @param ActionEvent An action event for us to handle. * * @return None */////////////////////////////////////////////////////////////// public void actionPerformed(ActionEvent evt) { // grab the object from which this ActionEvent originated Object source = evt.getSource(); // if the source object is the "Squares" button, then set the currentTool to // SQUARE. if((Button)source == squares) { selectedTool = SQUARE; } // otherwise if the source object is the "Circles" button, then set the currentTool to // CIRCLE. else if((Button)source == circles) { selectedTool = CIRCLE; } } ///////////////////////////////////////////////////////////////// // inner class drawingArea ///////////////////////////////////////////////////////////////// /** * This class handles the drawing and on-screen manipulation of * the canvas area. */////////////////////////////////////////////////////////////// class drawingArea extends Canvas implements MouseListener { ///////////////////////////////////////////////////////////////// // Constructor ///////////////////////////////////////////////////////////////// /** * Creates a new drawingArea and sets it up to handle its own * mouse events. * * @return drawingArea -- the new instance */////////////////////////////////////////////////////////////// public drawingArea() { addMouseListener(this); } ///////////////////////////////////////////////////////////////// // mousePressed ///////////////////////////////////////////////////////////////// /** * This method handles the case of a mouse down in the canvas. * It simply creates a new Square or Circle, adds it to the * objectList and then repaints. * * @param MouseEvent A mouse event for us to handle. * * @return None */////////////////////////////////////////////////////////////// public void mousePressed(MouseEvent evt) { int x = evt.getX(); int y = evt.getY(); // create and add a new object if(selectedTool == SQUARE) { objectList.addElement(new Square(x - 10, y - 10, 20, 20)); } else if(selectedTool == CIRCLE) { objectList.addElement(new Circle(x - 10, y - 10, 20, 20)); } // cause the canvas to redraw repaint(); } // the following four methods are currently included only to define the // MouseListener interface public void mouseClicked(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } ///////////////////////////////////////////////////////////////// // paint ///////////////////////////////////////////////////////////////// /** * This method is used to draw the contents of the canvas. It * simply loops through the objects in the objectList and calls * the draw method of each one. * * @param Graphics The on-screen graphics context for * the canvas. * * @return None */////////////////////////////////////////////////////////////// public void paint(Graphics g) { int count; GeometricObject temp; // loop through the objects and call their draw methods for(count = 0; count < objectList.size(); count++) { temp = (GeometricObject)objectList.elementAt(count); temp.draw(g); } } } }