/* * Remember, these lines just give us access to all of the * graphics functions, so we can take advantage of the * classes that have been defined by the good folks at Sun. */ import java.awt.*; import java.applet.*; import java.awt.event.*; /* * This is the class that defines the Sketching applet */ public class Sketching extends Applet implements MouseListener { /* * When the user release the mouse, we'll make changes to these * and then invoke repaint to install them * * This is a technique known as "double-buffering". It will * make this assignment easier to implement than drawign directly onto * the active Image. And, it is also better, because the active * Image will change, without the redraw being visible. */ private Graphics nextGraphics; private Image nextImage; /* * Dimensions of the applet, itself * These come from the HTML page's APPLET tag */ Dimension appletDim; /* * Start and finish control points for graphics objects, * such as line, oval, rectangle, etc. */ private Point start, finish; /* * This method is called when the applet is initialized */ public void init() { appletDim = getSize(); // size of the applet, itself /* * Create the buffer that we'll use to store out changes to * the screen, before we actually paint them onto the screen. * * This involves creating a new Image, and then extracting * the Graphics object, the actual screen-map, from it. */ nextImage = createImage (appletDim.width, appletDim.height); nextGraphics = nextImage.getGraphics(); /* * Add code here to set the foreground color to white, and then * to draw a box the size of the applet. Remember, we want * to draw to "nextGraphics". paint() is automatically called * after init(), so we don't need to call repaint() */ /* * Then, change the color back to something different, maybe black, * so that what is drawn on the screen will be visible. */ /* * Instruct the applet to watch for mouse events */ addMouseListener(this); } /* * This is used, sometimes automatically, and sometimes * explicitly by the program, to redraw the screen * In this case, we prepare "nextImage" in advance. * When we want to redraw the screen, for example after a mouse * event that adds an object, we adjust nextImage, and then * call repaint(), which in turn calls this method. */ public void paint(Graphics g) { g.drawImage (nextImage, 0, 0, this); } /* * If the mouse is pressed, it is a starting point. * So, we want to mark it with a dot and store it for later, * so we can draw the object */ public void mousePressed (MouseEvent me) { // Save the current position of the mouse into start = new Point (me.getX(), me.getY()); /* You might want to draw a dot so the user can see the original * point while moving the mouse to the second point * * If you want to do this, you can do it by drawing a line 1 pixel * long. You want to draw onto "nextGraphics" and then call * repaint() to install "nextgraphics" on the screen. * * Otherwise, you can just save the point and draw the object when * the user releases the mouse at the second point. */ } /* * When the mouse is released it is time to draw the object. * So, we find the point, set the color, and draw the right shape */ public void mouseReleased (MouseEvent me) { // Save the end point // Draw the shape or line onto "nextGraphics" // Call repaint, so the change can be seen. } /* * We didn't need these actions. But, we do need to * define them, or we haven't complied with the * MouseAction interface. */ // This is received, if the user momentarily presses the mouse button public void mouseClicked (MouseEvent me) {} // This is received, if the mouse enters the applet area from outside public void mouseEntered (MouseEvent me) {} // This is received, if the mouse exits the applet area from outside public void mouseExited (MouseEvent me) {} }