KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > demos > DrawRepl


1 // $Id: DrawRepl.java,v 1.4 2004/09/23 16:29:35 belaban Exp $
2

3 package org.jgroups.demos;
4
5
6 import org.jgroups.Channel;
7 import org.jgroups.JChannel;
8 import org.jgroups.blocks.GroupRequest;
9 import org.jgroups.blocks.MethodCall;
10 import org.jgroups.blocks.RpcDispatcher;
11
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.DataInputStream JavaDoc;
17 import java.io.DataOutputStream JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Random JavaDoc;
20
21
22
23
24 /**
25  * NOT SUPPORTED !
26  * Replicates the whiteboard demo by intercepting central AWT event queue and mcasting events to
27  * all members. Not very useful in all cases, e.g. when the "Leave" button is pressed, and this event
28  * is broadcast to all members, all members will leave ! This demo would clearly benefit from more work !
29  */

30 public class DrawRepl implements MouseMotionListener, WindowListener, ActionListener,
31                  Runnable JavaDoc {
32     private Graphics graphics=null;
33     private Frame mainFrame=null;
34     private Panel panel=null, sub_panel=null;
35     private final byte[] buf=new byte[128];
36     private final ByteArrayOutputStream JavaDoc out=new ByteArrayOutputStream JavaDoc();
37     private DataOutputStream JavaDoc outstream;
38     private ByteArrayInputStream JavaDoc inp;
39     private DataInputStream JavaDoc instream;
40     private int x, y;
41     private final Hashtable JavaDoc colors=new Hashtable JavaDoc();
42     private final Random JavaDoc random=new Random JavaDoc(System.currentTimeMillis());
43     private int col_val=1;
44     private Color current_color=Color.red;
45     private Button clear_button, leave_button;
46     private final String JavaDoc groupname="DrawReplGroup";
47     private final Font default_font=new Font("Helvetica",Font.PLAIN,12);
48
49     private EventQueue event_queue=null;
50     private Thread JavaDoc mythread=null;
51     private RpcDispatcher dispatcher;
52     private Channel channel;
53
54
55     public DrawRepl() {
56     colors.put(new Integer JavaDoc(1), Color.white);
57     colors.put(new Integer JavaDoc(2), Color.black);
58     colors.put(new Integer JavaDoc(3), Color.red);
59     colors.put(new Integer JavaDoc(4), Color.orange);
60     colors.put(new Integer JavaDoc(5), Color.green);
61     colors.put(new Integer JavaDoc(6), Color.magenta);
62     colors.put(new Integer JavaDoc(7), Color.cyan);
63     colors.put(new Integer JavaDoc(8), Color.blue);
64     mythread=new Thread JavaDoc(this);
65     try {
66         channel=new JChannel();
67         dispatcher=new RpcDispatcher(channel, null, null, this);
68         channel.connect(groupname);
69     }
70     catch(Exception JavaDoc e) {
71         System.err.println(e);
72         System.exit(0);
73     }
74     }
75
76
77     public static void main(String JavaDoc[] args) {
78     DrawRepl draw=new DrawRepl();
79     draw.go();
80     }
81
82
83     private Color SelectColor() {
84     col_val=(Math.abs(random.nextInt()) % 8)+1;
85     Color ret=(Color)colors.get(new Integer JavaDoc(col_val));
86     if(ret == null)
87         ret=Color.red;
88     return ret;
89     }
90
91
92
93     AWTEvent copyEvent(Component src, AWTEvent evt) {
94
95     if(evt instanceof MouseEvent) {
96         MouseEvent mev=(MouseEvent)evt;
97         return new MouseEvent(src, evt.getID(), mev.getWhen(), mev.getModifiers(),
98                   mev.getX(), mev.getY(), mev.getClickCount(),
99                   mev.isPopupTrigger());
100     }
101
102     if(evt instanceof KeyEvent) {
103         KeyEvent kev=(KeyEvent)evt;
104         return new KeyEvent(src, evt.getID(), kev.getWhen(), kev.getModifiers(),
105                 kev.getKeyCode(), kev.getKeyChar());
106     }
107
108     if(evt instanceof ActionEvent)
109         return new ActionEvent(src, evt.getID(), ((ActionEvent)evt).getActionCommand(),
110                    ((ActionEvent)evt).getModifiers());
111
112
113     if(evt instanceof PaintEvent)
114         return new PaintEvent(src, evt.getID(), ((PaintEvent)evt).getUpdateRect());
115
116
117     if(evt instanceof FocusEvent)
118         return new FocusEvent(src, evt.getID(), ((FocusEvent)evt).isTemporary());
119
120     if(evt instanceof ComponentEvent)
121         return new ComponentEvent(src, evt.getID());
122                 
123     return null;
124     }
125
126
127
128     void dispatch(Object JavaDoc src, AWTEvent evt) {
129     if (src instanceof Component)
130         ((Component)src).dispatchEvent(evt);
131     else if (src instanceof MenuComponent)
132         ((MenuComponent)src).dispatchEvent(evt);
133     else
134         System.err.println("++++++++++");
135     }
136     
137
138     public Component findComponent(Container parent, String JavaDoc comp_name) {
139     Component retval=null;
140
141     if(comp_name != null && comp_name.equals(parent.getName()))
142         return parent;
143
144     int ncomponents = parent.getComponentCount();
145         Component components[] = parent.getComponents();
146     for (int i = ncomponents-1 ; i >= 0; i--) {
147         Component comp = components[i], tmp;
148         if (comp != null) {
149         if(comp instanceof Container) {
150             retval=findComponent((Container)comp, comp_name);
151             if(retval != null)
152             return retval;
153         }
154         else if(comp_name.equals(comp.getName()))
155             return comp;
156         }
157     }
158     return retval;
159     }
160
161
162     // public void setSize(Integer x, Integer y) {
163
// mainFrame.setSize(new Dimension(x.intValue(), y.intValue()));
164
// }
165

166
167     /* Called by Dispatcher */
168     public void processEvent(String JavaDoc comp_name, AWTEvent evt) {
169     AWTEvent copy_evt=null;
170     Component SRC=findComponent(mainFrame, comp_name);
171     if(src == null) {
172         System.err.println("processEvent(): src is null");
173         return;
174     }
175
176     System.out.println("Received " + evt.getClass().getName());
177
178     copy_evt=copyEvent(src, evt);
179     if(copy_evt == null) {
180         System.err.println("copy_evt is NULL");
181         return;
182     }
183     dispatch(src, copy_evt);
184
185
186 // if(evt instanceof ComponentEvent && evt.getID() == ComponentEvent.COMPONENT_RESIZED) {
187
// Dimension dim=mainFrame.getSize();
188
// try {
189
// dispatcher.sendGetN(groupname, "setSize", new Integer(dim.height),
190
// new Integer(dim.width), 0, 0);
191
// }
192
// catch(Exception e) {
193
// System.err.println(e);
194
// }
195
// }
196
}
197
198
199     void processLocally(AWTEvent evt) {
200     dispatch(evt.getSource(), evt);
201     }
202
203
204
205     public void run() {
206     String JavaDoc comp_name;
207     
208     while(true) {
209         try {
210         AWTEvent evt=event_queue.getNextEvent();
211         Object JavaDoc obj=evt.getSource();
212         if(obj == null) {
213             System.err.println("src is NULL");
214             continue;
215         }
216
217         if(obj instanceof Component)
218             comp_name=((Component)obj).getName();
219         else if(obj instanceof MenuComponent)
220             comp_name=((MenuComponent)obj).getName();
221         else {
222             System.err.println("src is of type " + obj.getClass().getName());
223             continue;
224         }
225
226         if(evt instanceof FocusEvent || evt instanceof PaintEvent) {
227             System.out.println(evt.getClass().getName() + " not copied");
228             processLocally(evt);
229             continue;
230         }
231         System.out.println("MCasting "+evt.getClass().getName()+" event...");
232         MethodCall call = new MethodCall("processEvent", new Object JavaDoc[] {comp_name, evt},
233             new String JavaDoc[] {String JavaDoc.class.getName(), AWTEvent.class.getName()});
234         dispatcher.callRemoteMethods(null, call, GroupRequest.GET_NONE, 0);
235         }
236         catch(Exception JavaDoc e) {
237         System.err.println(e);
238         }
239     }
240     }
241
242
243
244     
245     public void go() {
246     mainFrame=new Frame();
247     panel=new Panel();
248     sub_panel=new Panel();
249     
250     event_queue=mainFrame.getToolkit().getSystemEventQueue();
251     mythread.start();
252     
253     mainFrame.setSize(200,200);
254     mainFrame.add("Center", panel);
255     clear_button=new Button("Clear");
256     clear_button.setFont(default_font);
257     clear_button.addActionListener(this);
258     leave_button=new Button("Exit");
259     leave_button.setFont(default_font);
260     leave_button.addActionListener(this);
261     sub_panel.add("South", clear_button);
262     sub_panel.add("South", leave_button);
263     mainFrame.add("South", sub_panel);
264
265     mainFrame.addWindowListener(this);
266     // mainFrame.addComponentListener(this);
267

268     panel.addMouseMotionListener(this);
269
270     mainFrame.setVisible(true);
271
272     graphics=panel.getGraphics();
273     current_color=SelectColor();
274     if(current_color == null)
275         current_color=Color.red;
276     graphics.setColor(current_color);
277     }
278
279
280
281     /* --------------- Callbacks --------------- */
282
283
284     public void mouseMoved(MouseEvent e) {
285     }
286
287     public void mouseDragged(MouseEvent e) {
288     x=e.getX();
289     y=e.getY();
290     graphics.fillOval(x, y, 10, 10);
291     }
292
293
294     public void clearPanel() {
295
296     System.out.println("CLEAR");
297
298     Rectangle bounds=panel.getBounds();
299     graphics.clearRect(0, 0, bounds.width, bounds.height);
300     }
301
302
303
304     public void windowActivated(WindowEvent e) {}
305     public void windowClosed(WindowEvent e) {}
306     public void windowClosing(WindowEvent e) {
307     System.exit(0);
308     }
309     public void windowDeactivated(WindowEvent e) {}
310     public void windowDeiconified(WindowEvent e) {}
311     public void windowIconified(WindowEvent e) {}
312     public void windowOpened(WindowEvent e) {}
313
314
315
316 // public void componentResized(ComponentEvent e) {
317
// System.out.println("RESIZED, size is " + mainFrame.getBounds());
318
// }
319

320 // public void componentMoved(ComponentEvent e) {
321
// System.out.println("MOVED, location is: " + mainFrame.getLocation());
322
// }
323

324 // public void componentShown(ComponentEvent e) {}
325

326 // public void componentHidden(ComponentEvent e) {}
327

328
329
330
331     public void actionPerformed(ActionEvent e) {
332     String JavaDoc command=e.getActionCommand();
333     if("Clear".equals(command))
334         clearPanel();
335     else if("Exit".equals(command)) {
336         mainFrame.setVisible(false);
337         System.exit(0);
338     }
339     else
340         System.out.println("Unknown action");
341     }
342
343
344 }
345
346
Popular Tags