1 24 25 package org.objectweb.tribe.demos.draw; 26 27 import java.awt.Color ; 28 import java.awt.Dimension ; 29 import java.awt.Font ; 30 import java.awt.Graphics ; 31 import java.awt.Image ; 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 import java.awt.event.ComponentAdapter ; 35 import java.awt.event.ComponentEvent ; 36 import java.awt.event.MouseEvent ; 37 import java.awt.event.MouseMotionListener ; 38 import java.net.InetAddress ; 39 import java.util.Random ; 40 41 import javax.swing.JButton ; 42 import javax.swing.JFrame ; 43 import javax.swing.JPanel ; 44 45 import org.objectweb.tribe.channel.ReliableGroupChannelWithGms; 46 import org.objectweb.tribe.channel.tcp.TcpChannelPool; 47 import org.objectweb.tribe.common.GroupIdentifier; 48 import org.objectweb.tribe.common.IpAddress; 49 import org.objectweb.tribe.gms.GroupMembershipService; 50 import org.objectweb.tribe.gms.discovery.UdpDiscoveryService; 51 52 62 public class WhiteBoard implements ActionListener 63 { 64 private static final String GROUP_NAME = "tribe.whiteboard"; 65 private ReliableGroupChannelWithGms channel = null; 66 private int memberSize = 1; 67 boolean first = true, 68 cummulative = true; 69 private JFrame mainFrame = null; 70 private JPanel subPanel = null; 71 private DrawPanel panel = null; 72 private JButton clearButton; 73 private JButton leaveButton; 74 private Random random = new Random (System 75 .currentTimeMillis()); 76 private final Font defaultFont = new Font ("Helvetica", 77 Font.PLAIN, 12); 78 private Color drawColor = selectColor(); 79 private Color backgroundColor = Color.white; 80 81 86 public WhiteBoard() throws Exception 87 { 88 final InetAddress MULTICAST_ADDRESS = InetAddress.getByName("224.7.65.23"); 89 final int MULTICAST_PORT = 2288; 90 final IpAddress MULTICAST_IP = new IpAddress(MULTICAST_ADDRESS, 91 MULTICAST_PORT); 92 final InetAddress REPLY_ADDRESS = InetAddress.getLocalHost(); 93 final int REPLY_PORT = 0; 94 final IpAddress REPLY_IP = new IpAddress(REPLY_ADDRESS, REPLY_PORT); 95 96 UdpDiscoveryService discovery = new UdpDiscoveryService(MULTICAST_IP, 97 REPLY_IP); 98 GroupMembershipService gms = new GroupMembershipService(REPLY_IP, 99 TcpChannelPool.getChannelPool(), discovery); 100 101 channel = new ReliableGroupChannelWithGms(gms); 102 } 104 105 110 public static void main(String [] args) 111 { 112 WhiteBoard whiteBoard = null; 113 114 try 115 { 116 whiteBoard = new WhiteBoard(); 117 whiteBoard.go(); 118 } 119 catch (Throwable e) 120 { 121 e.printStackTrace(); 122 System.exit(0); 123 } 124 } 125 126 131 private Color selectColor() 132 { 133 int red = (Math.abs(random.nextInt()) % 255); 134 int green = (Math.abs(random.nextInt()) % 255); 135 int blue = (Math.abs(random.nextInt()) % 255); 136 return new Color (red, green, blue); 137 } 138 139 144 public void go() throws Exception 145 { 146 channel.join(new GroupIdentifier(GROUP_NAME)); 147 148 mainFrame = new JFrame (); 149 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 150 panel = new DrawPanel(); 151 panel.setBackground(backgroundColor); 152 subPanel = new JPanel (); 153 mainFrame.getContentPane().add("Center", panel); 154 clearButton = new JButton ("Clear"); 155 clearButton.setFont(defaultFont); 156 clearButton.addActionListener(this); 157 leaveButton = new JButton ("Leave & Exit"); 158 leaveButton.setFont(defaultFont); 159 leaveButton.addActionListener(this); 160 subPanel.add("South", clearButton); 161 subPanel.add("South", leaveButton); 162 mainFrame.getContentPane().add("South", subPanel); 163 mainFrame.setBackground(backgroundColor); 164 clearButton.setForeground(Color.blue); 165 leaveButton.setForeground(Color.blue); 166 setTitle(); 167 mainFrame.pack(); 168 mainFrame.setLocation(15, 25); 169 mainFrame.setVisible(true); 170 mainLoop(); 171 } 172 173 178 void setTitle(String title) 179 { 180 String tmp = ""; 181 if (title != null) 182 { 183 mainFrame.setTitle(title); 184 } 185 else 186 { 187 tmp += " (" + memberSize + ") mbrs"; 190 mainFrame.setTitle(tmp); 191 } 192 } 193 194 197 void setTitle() 198 { 199 setTitle(null); 200 } 201 202 205 public void mainLoop() 206 { 207 Object msg = null; 208 WhiteBoardCommand comm; 209 boolean fl = true; 210 211 while (fl) 212 { 213 try 214 { 215 msg = channel.receive(); 216 217 227 comm = null; 228 229 if (msg instanceof WhiteBoardCommand) 230 comm = (WhiteBoardCommand) msg; 231 else 232 { 233 if (msg != null) 234 System.out.println("*** Draw.run(): msg is " + msg.getClass()); 235 else 236 System.out.println("*** Draw.run(): msg is null"); 237 continue; 238 } 239 240 switch (comm.mode) 241 { 242 case WhiteBoardCommand.DRAW : 243 if (panel != null) 244 panel.drawPoint(comm); 245 break; 246 case WhiteBoardCommand.CLEAR : 247 clearPanel(); 248 continue; 249 default : 250 System.err 251 .println("***** Draw.run(): received invalid draw command " 252 + comm.mode); 253 break; 254 } 255 256 } 257 catch (Exception e) 258 { 259 System.err.println(e); 260 continue; 261 } 262 } 263 } 264 265 266 267 270 public void clearPanel() 271 { 272 if (panel != null) 273 panel.clear(); 274 } 275 276 279 public void sendClearPanelMsg() 280 { 281 WhiteBoardCommand comm = new WhiteBoardCommand(); 282 283 try 284 { 285 channel.send(comm); 286 } 287 catch (Exception ex) 288 { 289 System.err.println(ex); 290 } 291 } 292 293 296 public void actionPerformed(ActionEvent e) 297 { 298 String command = e.getActionCommand(); 299 if (command.equals("Clear")) 300 sendClearPanelMsg(); 301 else if (command.equals("Leave & Exit")) 302 { 303 try 304 { 305 channel.close(); 306 } 307 catch (Exception ex) 308 { 309 System.err.println(ex); 310 } 311 mainFrame.setVisible(false); 312 mainFrame.dispose(); 313 System.exit(0); 314 } 315 else 316 System.out.println("Unknown action"); 317 } 318 319 326 private class DrawPanel extends JPanel implements MouseMotionListener 327 { 328 Dimension preferred_size = new Dimension (235, 170); 329 Image img = null; Dimension d, imgsize; 331 Graphics gr = null; 332 333 336 public DrawPanel() 337 { 338 createOffscreenImage(); 339 addMouseMotionListener(this); 340 addComponentListener(new ComponentAdapter () 341 { 342 public void componentResized(ComponentEvent e) 343 { 344 if (getWidth() <= 0 || getHeight() <= 0) 345 return; 346 createOffscreenImage(); 347 } 348 }); 349 } 350 351 354 void createOffscreenImage() 355 { 356 d = getSize(); 357 if (img == null || imgsize == null || imgsize.width != d.width 358 || imgsize.height != d.height) 359 { 360 img = createImage(d.width, d.height); 361 if (img != null) 362 gr = img.getGraphics(); 363 imgsize = d; 364 } 365 } 366 367 371 374 public void mouseDragged(MouseEvent e) 375 { 376 int x = e.getX(), y = e.getY(); 377 WhiteBoardCommand comm = new WhiteBoardCommand(x, y, drawColor.getRed(), 378 drawColor.getGreen(), drawColor.getBlue()); 379 380 try 381 { 382 channel.send(comm); 383 Thread.yield(); } 385 catch (Exception ex) 386 { 387 System.err.println(ex); 388 } 389 } 390 391 394 public void mouseMoved(MouseEvent e) 395 { 396 } 397 398 402 409 public void drawPoint(WhiteBoardCommand c) 410 { 411 if (c == null || gr == null) 412 return; 413 gr.setColor(new Color (c.r, c.g, c.b)); 414 gr.fillOval(c.x, c.y, 10, 10); 415 repaint(); 416 } 417 418 421 public void clear() 422 { 423 if (gr == null) 424 return; 425 gr.clearRect(0, 0, getSize().width, getSize().height); 426 repaint(); 427 } 428 429 432 public Dimension getPreferredSize() 433 { 434 return preferred_size; 435 } 436 437 440 public void paintComponent(Graphics g) 441 { 442 super.paintComponent(g); 443 if (img != null) 444 { 445 g.drawImage(img, 0, 0, null); 446 } 447 } 448 449 } 450 451 } 452 453 | Popular Tags |