import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; public class DrawProgram2 extends JApplet implements ActionListener { protected JButton drawRect, drawOval; ArrayList shapes = new ArrayList(); // array of colors: access with colors.length and colors[i] public static final Color[] colors = { Color.yellow, Color.black, Color.green, Color.blue, Color.orange, Color.magenta, Color.gray }; public DrawProgram2(){ drawRect = new JButton("Draw Rectangle"); drawRect.setActionCommand("Draw Rectangle"); drawOval = new JButton("Draw Oval"); drawOval.setActionCommand("Draw Oval"); drawRect.addActionListener(this); drawOval.addActionListener(this); add(drawRect); add(drawOval); } public void actionPerformed(ActionEvent e) { if ("Draw Rectangle".equals(e.getActionCommand())) { addShape(new RectShape()); System.out.println(shapes); } if ("Draw Oval".equals(e.getActionCommand())) { addShape(new OvalShape()); System.out.println(shapes); } } void addShape(Shape shape) { // Add the shape to the canvas, and set its size/position and color. // The shape is added at the top-left corner, with size 80-by-50. // Then redraw the canvas to show the newly added shape. shapes.add(shape); repaint(); } //////////////////////////////////////////// // Paint //////////////////////////////////////////// public void paintComponent(Graphics g) { } //////////////////////////////////////////// // Event Handlers //////////////////////////////////////////// public void onMousePressed(int x, int y) { System.out.println("mousePressed at " + x + "," + y); } public static void onMouseDragged(int x, int y) { System.out.println("mouseDragged at " + x + "," + y); } public void onMouseReleased(int x, int y) { System.out.println("mouseReleased at " + x + "," + y); } public void onKeyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); char keyChar = e.getKeyChar(); System.out.println("keyPressed, code = " + keyCode + ", char = '" + keyChar + "'"); } public void onTimer() { // System.out.println("onTimer called"); } //////////////////////////////////////////// // addEventListeners //////////////////////////////////////////// public void addEventListeners() { // add actions in response to mouse events addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { onMousePressed(e.getX(),e.getY()); } public void mouseReleased(MouseEvent e) { onMouseReleased(e.getX(),e.getY()); } }); // and for mouse motion events addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { onMouseDragged(e.getX(),e.getY()); } }); // and for keyboard events addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { onKeyPressed(e); } }); // and for timer events /* int timerMilliseconds = TIMER_DELAY; // 1000ms = 1 second new Timer(timerMilliseconds, new ActionListener() { public void actionPerformed(ActionEvent evt) { onTimer(); } }).start();*/ } //////////////////////////////////////////// // Main //////////////////////////////////////////// private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Draw Program"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. DrawProgram2 ContentPane = new DrawProgram2(); frame.setContentPane(ContentPane); ContentPane.setLayout(new FlowLayout()); //Display the window. frame.pack(); frame.setVisible(true); frame.setSize(500,500); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } static abstract class Shape { // A class representing shapes that can be displayed on a ShapeCanvas. // The subclasses of this class represent particular types of shapes. // When a shape is first constucted, it has height and width zero // and a default color of white. int left, top; // Position of top left corner of rectangle that bounds this shape. int width, height; // Size of the bounding rectangle. Color color = Color.white; // Color of this shape. void reshape(int left, int top, int width, int height) { // Set the position and size of this shape. this.left = left; this.top = top; this.width = width; this.height = height; } void moveBy(int dx, int dy) { // Move the shape by dx pixels horizontally and dy pixels veritcally // (by changing the position of the top-left corner of the shape). left += dx; top += dy; } void setColor(Color color) { // Set the color of this shape this.color = color; } boolean containsPoint(int x, int y) { // Check whether the shape contains the point (x,y). // By default, this just checks whether (x,y) is inside the // rectangle that bounds the shape. This method should be // overridden by a subclass if the default behaviour is not // appropriate for the subclass. if (x >= left && x < left+width && y >= top && y < top+height) return true; else return false; } abstract void draw(Graphics g); // Draw the shape in the graphics context g. // This must be overriden in any concrete subclass. } // end of class Shape static class RectShape extends Shape { // This class represents rectangle shapes. void draw(Graphics g) { g.setColor(color); g.fillRect(left,top,width,height); g.setColor(Color.black); g.drawRect(left,top,width,height); } } static class OvalShape extends Shape { // This class represents oval shapes. void draw(Graphics g) { g.setColor(color); g.fillOval(left,top,width,height); g.setColor(Color.black); g.drawOval(left,top,width,height); } } }