/** * RepaintApplet2.java * A somewhat better approach than RepaintApplet1.java. * Uses update() to automatically handle the background while paint() just * updates the red dot. * Note that setBackground(Color.yellow) is added to init(). Update() first fills in * the current background for the component associated with the graphics context and * then calls paint(). * If you wish to 'shortcircuit' this background fill, you have to override udate() * as shown in RepaintApplet2a.java. */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class RepaintApplet2 extends Applet { int mouseX = -10; int mouseY = -10; public void init() { addMouseListener(new HandleMouse()); setBackground(Color.yellow); } class HandleMouse extends MouseAdapter { public void mouseClicked(MouseEvent e) { Graphics g = getGraphics(); mouseX = e.getX(); mouseY = e.getY(); update(g); // Calls update directly: not a good idea } } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(mouseX-10, mouseY-10, 20, 20); } }