// file: Square.java // author: Robert M. Keller // purpose: minimal graphics and mouse-tracking applet // A square is drawn. // As various mouse events occur, the event and its coordinates appear // on the screen. // If the Erase button is clicked, the square is temporarily erased. // This applet also prescribes a model for the use of double-buffering to // avoid flicker: drawing occurs in an off-screen image buffer, which is then // painted onto the screen as needed. This also simplifies drawing, // since each event creates a blank slate and then draws onto it. import java.applet.*; // applet class import java.awt.*; // Abstract Window Toolkit public class Square extends Applet { Color backgroundColor = Color.white; // Colors of various features Color squareColor = Color.blue; Image image; // Image to be drawn on screen by paint method Graphics graphics; // Graphics part of image, acts as buffer Button eraseButton; // Button to erase square // Initialize the applet. public void init() { makeImageBuffer(); drawSquare(squareColor); add(eraseButton = new Button("Erase")); } // Draw a square. void drawSquare(Color color) { int x = size().width / 3; // size() is size of applet frame int y = size().height / 3; int width = x; int height = width; graphics.setColor(color); graphics.fillRect(x, y, width, height); } // show draws the mouse coordinates and square into the graphics buffer boolean show(String message, int x, int y) { clear(); drawSquare(squareColor); graphics.setColor(Color.black); graphics.drawString(message + " at (" + x + ", " + y + ")", 50, 100); repaint(); return true; } // mouseDown is called when the mouse button is depressed. public boolean mouseDown(Event e, int x, int y) { return show("mouseDown", x, y); } // mouseDrag is called when the mouse is dragged. public boolean mouseDrag(Event e, int x, int y) { return show("mouseDrag", x, y); } // mouseUp is called when the mouse button is released. public boolean mouseUp(Event v, int x, int y) { return show("mouseUp", x, y); } // handle erase button event public boolean action(Event event, Object arg) { if( event.target == eraseButton ) { drawSquare(backgroundColor); repaint(); } return super.action(event, arg); // Call super } // The following methods are "boilerplate" which you will probably // want to use as is. // Make off-screen image buffer based on size of the applet. void makeImageBuffer() { image = createImage(size().width, size().height); graphics = image.getGraphics(); clear(); } // clear clears the graphics image void clear() { graphics.setColor(backgroundColor); graphics.fillRect(0, 0, size().width, size().height); graphics.drawRect(0, 0, size().width-1, size().height-1); } // update is implicitly called when repaint() is called // g will be bound to the Graphics object in the Applet, // not the one in the image. paint will draw the image into g. public void update(Graphics g) { paint(g); } // paint(Graphics) is called by update(g) and whenever // the screen needs painting (such as when it is newly exposed) public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } }