KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > notification > whiteboard > BrushSizeDrawCanvas


1 package demo.notification.whiteboard;
2
3
4
5 import java.awt.Canvas JavaDoc;
6
7 import java.awt.image.ImageProducer JavaDoc;
8
9 import java.awt.event.MouseAdapter JavaDoc;
10
11 import java.awt.event.MouseEvent JavaDoc;
12
13 import java.awt.event.MouseMotionAdapter JavaDoc;
14
15 import java.awt.Graphics JavaDoc;
16
17
18
19 public class BrushSizeDrawCanvas extends Canvas JavaDoc {
20
21
22
23     WorkgroupController controller_;
24
25     ImageProducer JavaDoc imageProducer_;
26
27
28
29     // the last point
30

31     private int lastX,lastY;
32
33
34
35     // the drawing color
36

37     private int drawRed=255;
38
39     private int drawGreen=255;
40
41     private int drawBlue=255;
42
43
44
45     public BrushSizeDrawCanvas(WorkgroupController controller, int width, int height) {
46
47     super();
48
49     setSize(width,height);
50
51     controller_ = controller;
52
53     initialize();
54
55     }
56
57     
58
59     public void initialize() {
60
61     imageProducer_ = controller_.getImage().getProducer();
62
63     
64
65     // react on pressed mouse key to start drawing
66

67     addMouseListener(new MouseAdapter JavaDoc() {
68
69         public void mousePressed(MouseEvent JavaDoc e) {
70
71             lastX = e.getX();
72
73             lastY = e.getY();
74
75         }
76
77         });
78
79
80
81     // react on dragged mouse
82

83     addMouseMotionListener(new MouseMotionAdapter JavaDoc() {
84
85         public void mouseDragged(MouseEvent JavaDoc e) {
86
87             controller_.drawLine(lastX,lastY,
88
89                    e.getX(),e.getY(),
90
91                    drawRed,drawGreen,drawBlue);
92
93             lastX = e.getX();
94
95             lastY = e.getY();
96
97             repaint();
98
99         }
100
101         });
102
103     }
104
105
106
107     public void paint(Graphics JavaDoc g){
108
109     g.drawImage(createImage(imageProducer_), 0, 0, null);
110
111     }
112
113
114
115     public void update(Graphics JavaDoc g){
116
117     paint(g);
118
119     }
120
121     
122
123     void setDrawColor(int red,int green,int blue) {
124
125     drawRed=red;
126
127     drawGreen=green;
128
129     drawBlue=blue;
130
131     }
132
133
134
135     public void clearAll() {
136
137     controller_.clearAll();
138
139     repaint();
140
141     }
142
143
144
145     void setBrushSize(int size) {
146
147     controller_.setBrushSize(size);
148
149     }
150
151 }
152
153
Popular Tags