import java.awt.Color; import java.awt.Canvas; import java.awt.Button; import java.awt.Image; import java.awt.Graphics; import java.awt.Frame; import java.awt.event.*; import java.util.*; /** * Class ShapeCanvas: holds shapes in a custom drawn area * @author Barb Ericson */ public class ShapeCanvas extends Canvas implements ShapeInterface { ///////////////// Private Attributes ///////////////////////////// private List shapes = new ArrayList(); // a list of shapes private Shape currentShape = null; // current shape being dragged private String currShapeType = Shape.RECTANGLE; // default shape type private int width = 100; // canvas width private int height = 100; // canvas height private Color backgroundColor = Color.yellow; private Image backgroundImage = null; // background image for double buffering private Graphics backgroundG = null; // graphics context of background image /////////////////// Constructors ////////////////////////////////// /** A constructor that uses the default size */ public ShapeCanvas () { init(); } /** * A constructor that takes the width and height * @param width the width of the canvas * @param height the height of the canvas */ public ShapeCanvas(int width, int height) { // set the local variables this.width = width; this.height = height; init(); } ////////////////////// Private Methods //////////////////////////////// /* Method to initialize the shape canvas size and set the mouse listeners*/ private void init() { // set the size of the canvas to the current width and height setSize(width,height); // add the mouse listener and mouse motion listener addMouseListener(new MyMouseAdapter()); addMouseMotionListener(new MyMouseMotionAdapter()); // add a component listener addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { backgroundImage = null; } }); // set the background color setBackground(backgroundColor); } ///////////////////////// Public Methods /////////////////////////////// /** * Method to add a shape to the shape vector * @param shape the shape to add */ public void add(Shape shape) { // add the shape to the vector of shapes shapes.add(shape); // force a repaint to show the new shape repaint(); } /** * Method to remove a shape from the shape vector * @param shape the shape to remove */ public void remove(Shape shape) { // remove the shape from the vector of shapes shapes.remove(shape); // removes first one // force a repaint to show that it is gone repaint(); } /** * Method to remove a shape given the index * @param index the index of the shape in the shape vector that you * wish to remove */ public void remove(int index) { // remove the shape at the given index shapes.remove(index); // force a repaint to show it is gone repaint(); } /** * Update normally clears the background and calls paint * override it here to just call paint * @param g the graphics context on which to draw */ public void update(Graphics g) { paint(g); } /** * Method to paint the shape canvas and all objects in it * @param g the graphic context on which to paint */ public void paint (Graphics g) { Shape currShape; int height = getSize().height; int width = getSize().width; // if background image not created then create it if (backgroundImage == null) { // create the background image for double buffering backgroundImage = createImage(width,height); // get the graphics context of the background image backgroundG = backgroundImage.getGraphics(); } // clear the drawing area in the background image backgroundG.setColor(getBackground()); backgroundG.clearRect(0, 0, width, height); // loop through the shape objects and draw each one for (int i=0; i