/** * RepaintApplet1.java * A somewhat better approach than RepaintApplet.java. * Drawing is moved from the event method to the paint method * The paint() method is called directly (a bad policy) and the background * is explicitly set to yellow using the graphics context. * See RepaintApplet2.java for simplification using update() * USAGE: Note the red quarter circle when the applet comes up. * Click the applet. Circle moves to proper position. Iconify etc. * Works ok. Re-click the applet link. Starts again! */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class RepaintApplet1 extends Applet { private int mouseX = -20; private int mouseY = -20; // to avoid a "ghost" red circle when we start this applet // we choose initial coordinates outside the applet's area public void init() { addMouseListener(new HandleMouse()); } class HandleMouse extends MouseAdapter { public void mouseClicked(MouseEvent e) { Graphics g = getGraphics(); mouseX = e.getX(); mouseY = e.getY(); paint(g); // Direct call to paint() : a bad design } } public void paint(Graphics g) { g.setColor(Color.yellow); g.fillRect(0,0,getSize().width, getSize().height); g.setColor(Color.red); g.fillOval(mouseX-10, mouseY-10, 20, 20); } }