/** * This is example #1 out of 6, showing a sequence of improvements through * the progression of examples. The six files should be viewed in order. * * This file shows a bad way to handle drawing. The drawing code is outside * the 'paint()' method, which means the applet window layout is not displayed * when the applet is first loaded, and also is not restored after the window * is partially or fully covered and then uncovered. It also means that the * programmer's drawing does not coordinate with painting by the AWT GUI * thread. At this point this is clearly not yet a drawing program. */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class GraphicsDrawing1 extends Applet { public void init() { setSize(400, 400); addMouseListener(new HandleMouse()); } class HandleMouse extends MouseAdapter { public void mouseClicked(MouseEvent e) { Graphics g = getGraphics(); g.setColor(Color.yellow); g.fillRect(0, 0, getSize().width, getSize().height); g.setColor(Color.red); g.fillOval(e.getX() - 10, e.getY() - 10, 20, 20); } } }