KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > demos > applets > DrawApplet


1 // $Id: DrawApplet.java,v 1.3 2004/09/23 16:29:17 belaban Exp $
2

3 package org.jgroups.demos.applets;
4
5 import java.applet.Applet JavaDoc;
6 import java.awt.*;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.awt.event.MouseEvent JavaDoc;
10 import java.awt.event.MouseMotionListener JavaDoc;
11 import java.io.ByteArrayInputStream JavaDoc;
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.DataInputStream JavaDoc;
14 import java.io.DataOutputStream JavaDoc;
15 import java.util.Random JavaDoc;
16 import java.util.Vector JavaDoc;
17 import org.jgroups.*;
18
19
20
21
22 public class DrawApplet extends Applet JavaDoc implements Runnable JavaDoc, MouseMotionListener JavaDoc, ActionListener JavaDoc {
23     private Graphics graphics=null;
24     private Panel panel=null, sub_panel=null;
25     private final ByteArrayOutputStream JavaDoc out=new ByteArrayOutputStream JavaDoc();
26     private DataOutputStream JavaDoc outstream;
27     private DataInputStream JavaDoc instream;
28     private final Random JavaDoc random=new Random JavaDoc(System.currentTimeMillis());
29     private Button clear_button, leave_button;
30     private Label mbr_label;
31     private final Font default_font=new Font("Helvetica",Font.PLAIN,12);
32     private final String JavaDoc groupname="DrawGroup";
33     private Channel channel=null;
34     private Thread JavaDoc receiver=null;
35     private int member_size=1;
36     private int red=0, green=0, blue=0;
37     private Color default_color=null;
38
39     private final ChannelFactory factory=new JChannelFactory();
40     private String JavaDoc props="TUNNEL(router_host=janet;router_port=12002):" +
41                                      "PING(gossip_host=janet;gossip_port=12002):" +
42                                      "FD:STABLE:NAKACK:UNICAST:FRAG:FLUSH:GMS:VIEW_ENFORCER:QUEUE";
43
44     private final Vector JavaDoc members=new Vector JavaDoc();
45     private boolean fl=true;
46     
47
48
49  
50     
51     public void init() {
52     System.out.println("INIT");
53     setLayout(new BorderLayout());
54
55     String JavaDoc tmp_props=getParameter("properties");
56     if(tmp_props != null) {
57         System.out.println("Setting parameters " + tmp_props);
58         props=tmp_props;
59     }
60
61
62     try {
63         channel=factory.createChannel(props);
64         showStatus("Connecting to group " + groupname);
65         channel.connect(groupname);
66     }
67     catch(Exception JavaDoc e) {
68         System.err.println(e);
69     }
70     receiver=new Thread JavaDoc(this, "DrawThread");
71     receiver.start();
72     go();
73     }
74
75
76     
77     public void start() {
78     System.out.println("------- START");
79     }
80
81
82     
83
84
85     public void destroy() {
86     System.out.println("------- DESTROY");
87
88     if(receiver != null && receiver.isAlive()) {
89         fl=false;
90         receiver.interrupt();
91         try {receiver.join(1000);} catch(Exception JavaDoc ex) {}
92     }
93     receiver=null;
94     showStatus("Disconnecting from " + groupname);
95     channel.disconnect();
96     showStatus("Disconnected");
97     }
98
99
100     public void paint(Graphics g) {
101     Rectangle bounds=panel.getBounds();
102     Color old=graphics.getColor();
103
104     if(bounds == null || graphics == null)
105         return;
106
107     graphics.setColor(Color.black);
108     graphics.drawRect(0, 0, bounds.width-1, bounds.height-1);
109     graphics.setColor(old);
110     }
111
112
113     private void selectColor() {
114     red=(Math.abs(random.nextInt()) % 255);
115     green=(Math.abs(random.nextInt()) % 255);
116     blue=(Math.abs(random.nextInt()) % 255);
117     default_color=new Color(red, green, blue);
118     }
119
120
121
122     
123     public void go() {
124     try {
125         panel=new Panel();
126         sub_panel=new Panel();
127         resize(200, 200);
128         add("Center", panel);
129         clear_button=new Button("Clear");
130         clear_button.setFont(default_font);
131         clear_button.addActionListener(this);
132         leave_button=new Button("Exit");
133         leave_button.setFont(default_font);
134         leave_button.addActionListener(this);
135         mbr_label=new Label("0 mbr(s)");
136         mbr_label.setFont(default_font);
137         sub_panel.add("South", clear_button);
138         sub_panel.add("South", leave_button);
139         sub_panel.add("South", mbr_label);
140         add("South", sub_panel);
141         panel.addMouseMotionListener(this);
142         setVisible(true);
143         mbr_label.setText(member_size + " mbrs");
144         graphics=panel.getGraphics();
145         selectColor();
146         graphics.setColor(default_color);
147         panel.setBackground(Color.white);
148         clear_button.setForeground(Color.blue);
149         leave_button.setForeground(Color.blue);
150     }
151     catch(Exception JavaDoc e) {
152         System.err.println(e);
153         return;
154     }
155     }
156
157
158     public void run() {
159     Object JavaDoc tmp;
160     Message msg=null;
161     int my_x=10, my_y=10, r=0, g=0, b=0;
162         
163
164     while(fl) {
165         my_x=10;
166         my_y=10;
167         try {
168         tmp=channel.receive(0);
169         if(tmp instanceof View) {
170             viewAccepted((View)tmp);
171             continue;
172         }
173         if(!(tmp instanceof Message))
174             continue;
175         msg=(Message)tmp;
176
177         if(msg == null || msg.getLength() == 0) {
178             System.err.println("DrawApplet.run(): msg or msg.buffer is null !");
179             continue;
180         }
181
182         instream=new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(msg.getRawBuffer(), msg.getOffset(), msg.getLength()));
183         r=instream.readInt(); // red
184
if(r == -13) {
185             clearPanel();
186             continue;
187         }
188         g=instream.readInt(); // green
189
b=instream.readInt(); // blue
190
my_x=instream.readInt();
191         my_y=instream.readInt();
192         }
193         catch(ChannelNotConnectedException conn) {
194         break;
195         }
196         catch(Exception JavaDoc e) {
197         System.err.println(e);
198         }
199         if(graphics != null) {
200         graphics.setColor(new Color(r, g, b));
201         graphics.fillOval(my_x, my_y, 10, 10);
202         graphics.setColor(default_color);
203         }
204     }
205     }
206
207
208     /* --------------- Callbacks --------------- */
209
210
211     public void mouseMoved(MouseEvent JavaDoc e) {}
212
213     public void mouseDragged(MouseEvent JavaDoc e) {
214     int tmp[]=new int[1], x, y;
215
216     tmp[0]=0;
217     x=e.getX();
218     y=e.getY();
219
220     graphics.fillOval(x, y, 10, 10);
221
222     try {
223         out.reset();
224         outstream=new DataOutputStream JavaDoc(out);
225         outstream.writeInt(red);
226         outstream.writeInt(green);
227         outstream.writeInt(blue);
228         outstream.writeInt(x);
229         outstream.writeInt(y);
230         channel.send(new Message(null, null, out.toByteArray()));
231         out.reset();
232     }
233     catch(Exception JavaDoc ex) {
234         System.err.println(ex);
235     }
236     }
237
238
239     public void clearPanel() {
240     Rectangle bounds=null;
241     if(panel == null || graphics == null)
242         return;
243
244     bounds=panel.getBounds();
245     graphics.clearRect(1, 1, bounds.width-2, bounds.height-2);
246     
247
248     }
249
250
251     public void sendClearPanelMsg() {
252     int tmp[]=new int[1]; tmp[0]=0;
253
254     clearPanel();
255
256     try {
257         out.reset();
258         outstream=new DataOutputStream JavaDoc(out);
259         outstream.writeInt(-13);
260         channel.send(new Message(null, null, out.toByteArray()));
261         outstream.flush();
262     }
263     catch(Exception JavaDoc ex) {
264         System.err.println(ex);
265     }
266     }
267
268
269
270
271     public void actionPerformed(ActionEvent JavaDoc e) {
272     String JavaDoc command=e.getActionCommand();
273     if(command == "Clear") {
274         System.out.println("Members are " + members);
275         sendClearPanelMsg();
276     }
277     else if(command == "Exit") {
278         try {
279         destroy();
280         setVisible(false);
281         }
282         catch(Exception JavaDoc ex) {
283         System.err.println(ex);
284         }
285
286     }
287     else
288         System.out.println("Unknown action");
289     }
290
291
292     public void viewAccepted(View v) {
293     Vector JavaDoc mbrs=v.getMembers();
294     if(v != null) {
295         System.out.println("View accepted: " +v);
296         member_size=v.size();
297
298         if(mbr_label != null)
299         mbr_label.setText(member_size + " mbr(s)");
300
301         members.removeAllElements();
302         for(int i=0; i < mbrs.size(); i++)
303         members.addElement(mbrs.elementAt(i));
304     }
305     }
306
307
308
309
310
311 }
312
313
Popular Tags