1 package sample.jms.topics.draw; 2 3 48 49 import java.awt.BorderLayout ; 50 import java.awt.Color ; 51 import java.awt.Dimension ; 52 import java.awt.Graphics2D ; 53 import java.awt.event.ActionEvent ; 54 import java.awt.event.ActionListener ; 55 import java.awt.event.MouseEvent ; 56 import java.awt.event.MouseMotionListener ; 57 import java.awt.event.WindowEvent ; 58 import java.awt.event.WindowListener ; 59 import java.awt.image.BufferedImage ; 60 import java.io.ByteArrayOutputStream ; 61 import java.io.DataOutputStream ; 62 import java.io.IOException ; 63 import java.util.HashMap ; 64 import java.util.Iterator ; 65 import java.util.Map ; 66 67 import javax.jms.BytesMessage ; 68 import javax.jms.JMSException ; 69 import javax.jms.Session ; 70 import javax.jms.TopicConnectionFactory ; 71 import javax.swing.ButtonGroup ; 72 import javax.swing.ImageIcon ; 73 import javax.swing.JFrame ; 74 import javax.swing.JLabel ; 75 import javax.swing.JMenu ; 76 import javax.swing.JMenuBar ; 77 import javax.swing.JRadioButtonMenuItem ; 78 import javax.swing.SwingUtilities ; 79 80 import org.mr.MantaAgent; 81 import org.mr.api.jms.MantaTopicConnectionFactory; 82 83 84 88 89 90 public class DrawAgent extends JFrame 91 implements WindowListener , MouseMotionListener , javax.jms.MessageListener { 92 93 public static final Map nameToColorMap; 94 95 static { 96 nameToColorMap = new HashMap (); 97 nameToColorMap.put("Blue", Color.blue); 98 nameToColorMap.put("Red", Color.red); 99 nameToColorMap.put("Green", Color.green); 100 nameToColorMap.put("Yellow", Color.yellow); 101 nameToColorMap.put("Black", Color.black); 102 } 103 104 private static final int MESSAGE_TTL = 6000000; 105 106 private ByteArrayOutputStream bout = null; 107 private DataOutputStream dout = null; 108 109 private javax.jms.TopicConnection connect = null; 110 private javax.jms.TopicSession pubSession = null; 111 private javax.jms.TopicPublisher publisher = null; 112 private javax.jms.TopicSession subSession = null; 113 private javax.jms.TopicSubscriber subscriber = null; 114 115 protected final int BUFFER_SIZE = 32*1024; 116 117 protected BufferedImage onscreenImage = null; 118 protected Graphics2D onscreen = null; 119 protected Color background = Color.WHITE; 120 protected String myColor = null; 121 protected int appHight = 127; 122 protected int appWidth = 127; 123 protected JLabel label = null; 124 125 protected Map peerDataMap = null; 126 protected Map peerColorMap = null; 127 128 private Runnable painter = null; 129 130 131 public DrawAgent() { 132 super("DrawAgent"); 133 134 painter = new Runnable () { 135 public void run() { 136 repaint(); 137 } 138 }; 139 initUI(); 141 142 peerDataMap = new HashMap (); 144 peerColorMap = new HashMap (); 145 146 bout = new ByteArrayOutputStream (BUFFER_SIZE); 148 dout = new DataOutputStream (bout); 149 150 this.initJMS(); 152 } 153 154 private void initUI() { 156 onscreenImage = new BufferedImage (appWidth, appHight, BufferedImage.TYPE_INT_RGB); 157 onscreen = (Graphics2D ) onscreenImage.getGraphics(); 158 onscreen.setColor((Color )nameToColorMap.get(myColor)); 159 clear(); 160 ImageIcon icon = new ImageIcon (onscreenImage); 161 this.getContentPane().setLayout(new BorderLayout (0, 0)); 162 label = new JLabel (icon); 163 label.setPreferredSize(new Dimension (appWidth, appHight)); 164 this.getContentPane().add(label, BorderLayout.CENTER); 165 this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 166 this.addWindowListener(this); 167 label.addMouseMotionListener(this); 168 chooseColor(); 169 this.createMenu(); 170 this.pack(); 171 } 172 173 private void createMenu() { 175 JMenuBar menuBar = new JMenuBar (); 176 JMenu colorMenu = new JMenu ("Color"); 177 menuBar.add(colorMenu); 178 final ButtonGroup group = new ButtonGroup (); 179 JRadioButtonMenuItem item = null; 180 181 ActionListener radioHandler = new ActionListener () { 182 public void actionPerformed(ActionEvent e) { 183 myColor = group.getSelection().getActionCommand(); 184 onscreen.setColor((Color )nameToColorMap.get(myColor)); 185 sendMessage(); 186 drawData(); 187 } 188 }; 189 Iterator i = nameToColorMap.keySet().iterator(); 190 while (i.hasNext()) { 191 String name = i.next().toString(); 192 item = new JRadioButtonMenuItem (name); 193 item.setActionCommand(name); 194 if (name.equals(this.myColor)) { 195 item.setSelected(true); 196 } 197 group.add(item); 198 item.addActionListener(radioHandler); 199 colorMenu.add(item); 200 } 201 this.setJMenuBar(menuBar); 202 } 203 204 private void initJMS() { 206 try { 208 TopicConnectionFactory factory; 209 factory = new MantaTopicConnectionFactory(); 210 connect = factory.createTopicConnection(); 211 pubSession = connect.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 212 subSession = connect.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 213 } catch (javax.jms.JMSException jmse) { 214 System.err.println("error while creating connection - " + jmse.toString()); 215 jmse.printStackTrace(); 216 System.exit(1); 217 } 218 219 try { 221 javax.jms.Topic topic = pubSession.createTopic("Draw"); 222 publisher = pubSession.createPublisher(topic); 223 subscriber = subSession.createSubscriber(topic); 224 subscriber.setMessageListener(this); 225 226 connect.start(); 228 } catch (javax.jms.JMSException jmse) { 229 jmse.printStackTrace(); 230 } 231 } 232 233 private void chooseColor() { 235 int num = (int)(Math.random()*10%5); 236 Iterator i = nameToColorMap.keySet().iterator(); 237 Object o = null; 238 while (num >= 0) { 239 o = i.next(); 240 num--; 241 } 242 this.myColor = o.toString(); 243 } 244 245 protected void drawData() { 247 clear(); 248 Iterator it = this.peerDataMap.keySet().iterator(); 249 while (it.hasNext()) { 250 String peerId = (String )it.next(); 251 byte[] data = (byte[])peerDataMap.get(peerId); 252 String peerColor = (String )peerColorMap.get(peerId); 253 onscreen.setColor((Color )nameToColorMap.get(peerColor)); 254 255 for (int i = 0 ; i < data.length ; i++) { 256 onscreen.drawRect(data[i],data[++i],1,1); 257 } 258 } 259 SwingUtilities.invokeLater(painter); 260 } 261 262 protected void clear(){ 264 onscreen.setColor(background); 266 onscreen.fillRect(0,0,appWidth,appHight); 267 SwingUtilities.invokeLater(painter); 268 onscreen.setColor((Color )nameToColorMap.get(myColor)); 269 } 270 271 272 276 public void onMessage(javax.jms.Message aMessage) 277 { 278 javax.jms.BytesMessage bytesMessage = null; 279 int size = 0; 280 281 try { 282 bytesMessage = (javax.jms.BytesMessage ) aMessage; 283 size = (int)bytesMessage.getBodyLength(); 284 byte[] peerData = new byte[size]; 285 bytesMessage.readBytes(peerData, size); 287 String peerId = bytesMessage.getStringProperty("id"); 288 String peerColor = bytesMessage.getStringProperty("color"); 289 this.peerDataMap.put(peerId, peerData); 290 this.peerColorMap.put(peerId, peerColor); 291 } catch (javax.jms.JMSException jmse) { 292 jmse.printStackTrace(); 293 return; 294 } catch (java.lang.RuntimeException rte) { 295 rte.printStackTrace(); 296 return; 297 } catch (java.lang.Exception e) { 298 e.printStackTrace(); 299 return; 300 } 301 this.drawData(); 302 } 303 304 public void exit() { 306 try { 307 connect.close(); 308 } catch (javax.jms.JMSException jmse) { 309 jmse.printStackTrace(); 310 } catch (Exception e) { 311 e.printStackTrace(); 312 } 313 try { 314 dout.close(); 315 } catch (IOException e) { 316 e.printStackTrace(); 317 } catch (Exception e) { 318 e.printStackTrace(); 319 } 320 try { 321 bout.close(); 322 } catch (IOException e) { 323 } catch (Exception e) { 324 e.printStackTrace(); 325 } 326 bout = null; 327 dout = null; 328 connect = null; 329 330 System.exit(0); 331 } 332 333 private void sendMessage() { 335 try { 336 BytesMessage msg = pubSession.createBytesMessage(); 337 byte[] data = bout.toByteArray(); 338 int size = bout.size(); 339 msg.writeBytes(data, 0, size); 340 msg.setStringProperty("id", MantaAgent.getInstance().getAgentName()); 341 msg.setStringProperty("color", myColor); 342 publisher.publish(msg, 344 javax.jms.DeliveryMode.NON_PERSISTENT, 345 javax.jms.Message.DEFAULT_PRIORITY, 346 MESSAGE_TTL); 347 } catch (JMSException ex) { 348 System.out.println("Error sending message"); 349 ex.printStackTrace(); 350 } 351 } 352 353 public void mouseDragged(MouseEvent e) { 355 try { 357 dout.writeByte(e.getX()); 358 dout.writeByte(e.getY()); 359 } catch (IOException ex) { 360 } 361 362 drawData(); 364 365 sendMessage(); 367 } 368 public void mouseMoved(MouseEvent e) {} 369 370 public void windowClosing(WindowEvent arg0) { 372 System.out.println("Closing the window"); 373 this.exit(); 374 } 375 public void windowActivated(WindowEvent arg0) {} 376 public void windowClosed(WindowEvent arg0) {} 377 public void windowDeactivated(WindowEvent arg0) {} 378 public void windowDeiconified(WindowEvent arg0) {} 379 public void windowIconified(WindowEvent arg0) {} 380 public void windowOpened(WindowEvent arg0) {} 381 382 383 public static void main(String [] args) throws IOException { 384 final DrawAgent agent = new DrawAgent(); 385 SwingUtilities.invokeLater(new Runnable () { 386 public void run() { 387 agent.setVisible(true); 388 } 389 }); 390 } 391 392 } 393 | Popular Tags |