/** * This is example #6 out of 6, showing a sequence of improvements through * the progression of examples. The six files should be viewed in order. * * This file removes the 'update()' method that was created in an earlier * example, thus relying once again on the built-in (default) 'update()' * method. This means that this is no longer a drawing program, since the * default 'update()' method paints over the background before calling the * 'paint()' method, effectively erasing all previous red dots. This * program now simply allows the user to "drag" a red dot around on the * applet window. More accurately, the program continuously erases the * previous dot and draws a new dot under the user's mouse pointer. */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class GraphicsDrawing6 extends Applet { int mouseX = -10; int mouseY = -10; public void init() { setSize(400, 400); setBackground(Color.yellow); addMouseMotionListener(new HandleMouseMotion()); } class HandleMouseMotion extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { mouseX = e.getX(); mouseY = e.getY(); repaint(); } } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(mouseX - 10, mouseY - 10, 20, 20); } }