import java.awt.*; public class MouseCanvas extends java.applet.Applet { boolean boolDraw; int lastx, lasty; int newx, newy; public void init() { add(new Canvas()); boolDraw = false; lastx = size().width / 2; lasty = size().height / 2; newx = newy = 0; } public boolean mouseEnter(Event e, int x, int y) { boolDraw = true; repaint(); return true; } public boolean mouseExit(Event e, int x, int y) { boolDraw = false; repaint(); return true; } public boolean mouseMove(Event e, int x, int y) { newx = x; newy = y; repaint(); return true; } public boolean mouseDown(Event e, int x, int y) { lastx = newx; lasty = newy; repaint(); return true; } public void paint(Graphics g) { if (boolDraw) g.drawLine(lastx, lasty, newx, newy); } public Dimension preferredSize() { return new Dimension(400, 100); } public static void main(String [] args) { Frame f = new Frame("Mouse Canvas"); MouseCanvas mc = new MouseCanvas(); mc.init(); f.add("Center", mc); f.pack(); f.show(); } }