/* This applet demonstrates how an applet can respond when the user drags the mouse. The user can draw on the applet by dragging the mouse. Right-click on the applet to clear it. (This applet does not preperly deal with changes in size.) */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class applet_draw extends Applet implements MouseListener, MouseMotionListener { Image offscreen; // an offscreen image, to hold a copy of the sketch for repainting Graphics bufferGraphics; // graphics context for drawing to offscreen public void init() { // Initialize the applet. setBackground(Color.white); addMouseListener(this); // Register the applet to receive addMouseMotionListener(this); // mouse events from itself. } public void update(Graphics g) { // override this to avoid erasing the applet before drawwing paint(g); } public void paint(Graphics g) { // copy the off-screen image to the applet, creating it first // if it does not already exist if (offscreen == null) makeoffscreen(); g.drawImage(offscreen,0,0,this); } void makeoffscreen() { // create the off-screen image and graphics context offscreen = createImage(getSize().width,getSize().height); bufferGraphics = offscreen.getGraphics(); clearImage(); } void clearImage() { // make the off-screen image into a white rectagle with a red border bufferGraphics.setColor(Color.white); bufferGraphics.fillRect(0,0,getSize().width,getSize().height); bufferGraphics.setColor(Color.red); bufferGraphics.drawRect(0,0,getSize().width - 1,getSize().height - 1); bufferGraphics.setColor(Color.black); } void drawLine(int x1, int y1, int x2, int y2) { // Draw a line from (x1,y1) to (x2,y2) both on the off-screen // image and on the applet itself. This keeps the two // images in proper sync. bufferGraphics.drawLine(x1,y1,x2,y2); Graphics g = getGraphics(); // Graphics context for drawing to the applet. g.drawLine(x1,y1,x2,y2); g.dispose(); } boolean dragging = false; // This is true when a drag is in progress. int prev_x; // Previous position of cursor during a drag. int prev_y; public void mousePressed(MouseEvent evt) { // If user right-clicks the applet, clear the image; // otherwise start drawing by setting dragging = true if (offscreen == null) makeoffscreen(); if (evt.isMetaDown()) { clearImage(); repaint(); } else { dragging = true; prev_x = evt.getX(); prev_y = evt.getY(); } } public void mouseDragged(MouseEvent evt) { // Draw a line from previous mouse position to current // position, but only if a drag is in progress. if (dragging) { int new_x = evt.getX(); int new_y = evt.getY(); drawLine(prev_x, prev_y, new_x, new_y); prev_x = new_x; prev_y = new_y; } } public void mouseReleased(MouseEvent evt) { // Finish off the drag, if one is in progress. if (dragging) { int new_x = evt.getX(); int new_y = evt.getY(); drawLine(prev_x, prev_y, new_x, new_y); dragging = false; } } public void mouseClicked(MouseEvent evt) { } // required by listener interface public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseMoved(MouseEvent evt) { } }