1
2
3
4
5
6
7import java.applet.*;
8import java.awt.*;
9import java.awt.event.*;
10
11public
12class Scribble
13 extends Applet
14{
15 int lastX;
16 int lastY;
17
18
19
20
21 public void init() {
22
23
24
25
26 Button b = new Button("Clear");
27
28 setBackground(Color.yellow);
29
30
31
32
33
34 addMouseListener
35 (new MouseAdapter() {
36 public
37 void mousePressed(MouseEvent e) {
38 lastX = e.getX();
39 lastY = e.getY();
40 }
41 });
42
43
44
45 addMouseMotionListener
46 (new MouseMotionAdapter() {
47 public
48 void mouseDragged(MouseEvent e) {
49 Graphics g = getGraphics();
50 int x = e.getX();
51 int y = e.getY();
52
53 g.setColor(Color.black);
54 g.drawLine(lastX, lastY, x, y);
55
56 lastX = x; lastY = y;
57 }
58 });
59
60
61
62
63 b.addActionListener
64 (new ActionListener() {
65 public
66 void actionPerformed(ActionEvent e) {
67 Graphics g = getGraphics();
68 g.setColor(getBackground());
69 g.fillRect(0, 0, getSize().width, getSize().height);
70 }
71 });
72
73
74
75 add(b);
76 }
77}
78
79