1 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 ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.DataInputStream ; 17 import java.io.DataOutputStream ; 18 import java.util.Hashtable ; 19 import java.util.Random ; 20 21 22 23 24 30 public class DrawRepl implements MouseMotionListener, WindowListener, ActionListener, 31 Runnable { 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 out=new ByteArrayOutputStream (); 37 private DataOutputStream outstream; 38 private ByteArrayInputStream inp; 39 private DataInputStream instream; 40 private int x, y; 41 private final Hashtable colors=new Hashtable (); 42 private final Random random=new Random (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 groupname="DrawReplGroup"; 47 private final Font default_font=new Font("Helvetica",Font.PLAIN,12); 48 49 private EventQueue event_queue=null; 50 private Thread mythread=null; 51 private RpcDispatcher dispatcher; 52 private Channel channel; 53 54 55 public DrawRepl() { 56 colors.put(new Integer (1), Color.white); 57 colors.put(new Integer (2), Color.black); 58 colors.put(new Integer (3), Color.red); 59 colors.put(new Integer (4), Color.orange); 60 colors.put(new Integer (5), Color.green); 61 colors.put(new Integer (6), Color.magenta); 62 colors.put(new Integer (7), Color.cyan); 63 colors.put(new Integer (8), Color.blue); 64 mythread=new Thread (this); 65 try { 66 channel=new JChannel(); 67 dispatcher=new RpcDispatcher(channel, null, null, this); 68 channel.connect(groupname); 69 } 70 catch(Exception e) { 71 System.err.println(e); 72 System.exit(0); 73 } 74 } 75 76 77 public static void main(String [] 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 (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 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 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 166 167 168 public void processEvent(String 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 } 197 198 199 void processLocally(AWTEvent evt) { 200 dispatch(evt.getSource(), evt); 201 } 202 203 204 205 public void run() { 206 String comp_name; 207 208 while(true) { 209 try { 210 AWTEvent evt=event_queue.getNextEvent(); 211 Object 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 [] {comp_name, evt}, 233 new String [] {String .class.getName(), AWTEvent.class.getName()}); 234 dispatcher.callRemoteMethods(null, call, GroupRequest.GET_NONE, 0); 235 } 236 catch(Exception 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 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 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 320 324 326 328 329 330 331 public void actionPerformed(ActionEvent e) { 332 String 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 |