1 24 25 package utobcast.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.io.ByteArrayOutputStream ; 39 import java.io.ObjectOutputStream ; 40 import java.util.Map ; 41 import java.util.Random ; 42 43 import javax.swing.JButton ; 44 import javax.swing.JFrame ; 45 import javax.swing.JPanel ; 46 47 import org.objectweb.dream.AbstractComponent; 48 import org.objectweb.dream.Push; 49 import org.objectweb.dream.PushException; 50 import org.objectweb.dream.message.MessageTypeImpl; 51 import org.objectweb.dream.message.Message; 52 import org.objectweb.dream.message.MessageType; 53 import org.objectweb.dream.message.manager.MessageManager; 54 import org.objectweb.dream.util.Error; 55 import org.objectweb.fractal.api.Component; 56 import org.objectweb.fractal.api.NoSuchInterfaceException; 57 import org.objectweb.fractal.api.control.IllegalBindingException; 58 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 59 import org.objectweb.fractal.util.Fractal; 60 61 66 public class DrawImpl extends AbstractComponent implements ActionListener , Push 67 { 68 private ByteArrayOutputStream out = new ByteArrayOutputStream (); 69 private String groupname = "DrawGroupDemo"; 70 private int memberSize = 1; 71 boolean first = true, cummulative = true; 72 private JFrame mainFrame = null; 73 private JPanel subPanel = null; 74 private DrawPanel panel = null; 75 private JButton clearButton, leaveButton; 76 private Random random = new Random (System 77 .currentTimeMillis()); 78 private final Font defaultFont = new Font ("Helvetica", Font.PLAIN, 79 12); 80 private Color drawColor = selectColor(), 81 backgroundColor = Color.white; 82 boolean noChannel = false; 83 84 Push outPushItf; 85 MessageManager messageManagerItf; 86 87 Component nodeComponentItf; 88 MessageType msgType = new MessageTypeImpl( 89 DrawCommandChunk.DEFAULT_NAME, 90 DrawCommandChunk.TYPE); 91 92 96 99 public void startFc() throws IllegalLifeCycleException 100 { 101 super.startFc(); 102 103 mainFrame = new JFrame (); 104 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 105 panel = new DrawPanel(); 106 panel.setBackground(backgroundColor); 107 subPanel = new JPanel (); 108 mainFrame.getContentPane().add("Center", panel); 109 clearButton = new JButton ("Clear"); 110 clearButton.setFont(defaultFont); 111 clearButton.addActionListener(this); 112 leaveButton = new JButton ("Leave & Exit"); 113 leaveButton.setFont(defaultFont); 114 leaveButton.addActionListener(this); 115 subPanel.add("South", clearButton); 116 subPanel.add("South", leaveButton); 117 mainFrame.getContentPane().add("South", subPanel); 118 mainFrame.setBackground(backgroundColor); 119 clearButton.setForeground(Color.blue); 120 leaveButton.setForeground(Color.blue); 121 mainFrame.setTitle("Draw example"); 122 mainFrame.pack(); 123 mainFrame.setLocation(15, 25); 124 mainFrame.setVisible(true); 125 } 126 127 131 135 public void push(Message message, Map context) throws PushException 136 { 137 138 DrawCommand comm = ((DrawCommandChunk) message 139 .getChunk(DrawCommandChunk.DEFAULT_NAME)).getDrawCommand(); 140 141 switch (comm.mode) 142 { 143 case DrawCommand.DRAW : 144 if (panel != null) 145 panel.drawPoint(comm); 146 break; 147 case DrawCommand.CLEAR : 148 clearPanel(); 149 break; 150 default : 151 System.err 152 .println("***** DrawImpl.run(): received invalid draw command " 153 + comm.mode); 154 break; 155 } 156 messageManagerItf.deleteMessage(message); 157 } 158 159 163 private Color selectColor() 164 { 165 int red = (Math.abs(random.nextInt()) % 255); 166 int green = (Math.abs(random.nextInt()) % 255); 167 int blue = (Math.abs(random.nextInt()) % 255); 168 return new Color (red, green, blue); 169 } 170 171 void clearPanel() 172 { 173 if (panel != null) 174 panel.clear(); 175 } 176 177 void sendClearPanelMsg() 178 { 179 int tmp[] = new int[1]; 180 tmp[0] = 0; 181 DrawCommand comm = new DrawCommand(DrawCommand.CLEAR); 182 ObjectOutputStream os; 183 184 Message msg = messageManagerItf.createMessage(msgType); 185 DrawCommandChunk chunk = (DrawCommandChunk) msg 186 .getChunk(DrawCommandChunk.DEFAULT_NAME); 187 chunk.setDrawCommand(comm); 188 try 189 { 190 outPushItf.push(msg, null); 191 } 192 catch (PushException e) 193 { 194 Error.error("Unable to send clear panel message ", logger, e); 195 } 196 } 197 198 201 public void actionPerformed(ActionEvent e) 202 { 203 String command = e.getActionCommand(); 204 if (command.equals("Clear")) 205 { 206 if (noChannel) 207 { 208 clearPanel(); 209 return; 210 } 211 212 sendClearPanelMsg(); 213 214 } 215 else if (command.equals("Leave & Exit")) 216 { 217 if (!noChannel) 218 { 219 try 220 { 221 Runnable stopNode = new StopNode(); 222 new Thread (stopNode).start(); 223 } 224 catch (Exception ex) 225 { 226 System.err.println(ex); 227 } 228 } 229 mainFrame.setVisible(false); 230 mainFrame.dispose(); 231 } 233 else 234 System.out.println("Unknown action"); 235 } 236 237 241 private class DrawPanel extends JPanel implements MouseMotionListener 242 { 243 Dimension preferredSize = new Dimension (235, 170); 244 Image img = null; Dimension d, imgsize; 246 Graphics gr = null; 247 248 public DrawPanel() 249 { 250 createOffscreenImage(); 251 addMouseMotionListener(this); 252 addComponentListener(new ComponentAdapter () 253 { 254 public void componentResized(ComponentEvent e) 255 { 256 if (getWidth() <= 0 || getHeight() <= 0) 257 return; 258 createOffscreenImage(); 259 } 260 }); 261 } 262 263 void createOffscreenImage() 264 { 265 d = getSize(); 266 if (img == null || imgsize == null || imgsize.width != d.width 267 || imgsize.height != d.height) 268 { 269 img = createImage(d.width, d.height); 270 if (img != null) 271 gr = img.getGraphics(); 272 imgsize = d; 273 } 274 } 275 276 283 public void drawPoint(DrawCommand c) 284 { 285 if (c == null || gr == null) 286 return; 287 gr.setColor(new Color (c.r, c.g, c.b)); 288 gr.fillOval(c.x, c.y, 10, 10); 289 repaint(); 290 } 291 292 public void clear() 293 { 294 if (gr == null) 295 return; 296 gr.clearRect(0, 0, getSize().width, getSize().height); 297 repaint(); 298 } 299 300 public Dimension getPreferredSize() 301 { 302 return preferredSize; 303 } 304 305 public void paintComponent(Graphics g) 306 { 307 super.paintComponent(g); 308 if (img != null) 309 { 310 g.drawImage(img, 0, 0, null); 311 } 312 } 313 314 318 public void mouseMoved(MouseEvent e) 319 { 320 } 321 322 public void mouseDragged(MouseEvent e) 323 { 324 ObjectOutputStream os; 325 int x = e.getX(), y = e.getY(); 326 DrawCommand comm = new DrawCommand(DrawCommand.DRAW, x, y, drawColor 327 .getRed(), drawColor.getGreen(), drawColor.getBlue()); 328 329 if (noChannel) 330 { 331 drawPoint(comm); 332 return; 333 } 334 335 Message msg = messageManagerItf.createMessage(msgType); 336 DrawCommandChunk chunk = (DrawCommandChunk) msg 337 .getChunk(DrawCommandChunk.DEFAULT_NAME); 338 chunk.setDrawCommand(comm); 339 try 340 { 341 outPushItf.push(msg, null); 342 } 343 catch (PushException e1) 344 { 345 346 } 347 } 348 349 } 350 351 355 private class StopNode implements Runnable 356 { 357 358 361 public void run() 362 { 363 try 364 { 365 Fractal.getLifeCycleController(nodeComponentItf).stopFc(); 366 } 367 catch (Exception e) 368 { 369 e.printStackTrace(); 370 } 371 } 372 373 } 374 375 379 382 public String [] listFc() 383 { 384 return new String []{Push.OUT_PUSH_ITF_NAME, MessageManager.ITF_NAME, 385 "node-c"}; 386 } 387 388 392 public void bindFc(String clientItfName, Object serverItf) 393 throws NoSuchInterfaceException, IllegalBindingException, 394 IllegalLifeCycleException 395 { 396 super.bindFc(clientItfName, serverItf); 397 if (clientItfName.equals(Push.OUT_PUSH_ITF_NAME)) 398 { 399 outPushItf = (Push) serverItf; 400 } 401 else if (clientItfName.equals(MessageManager.ITF_NAME)) 402 { 403 messageManagerItf = (MessageManager) serverItf; 404 } 405 else if (clientItfName.equals("node-c")) 406 { 407 nodeComponentItf = (Component) serverItf; 408 } 409 } 410 411 } 412 413 | Popular Tags |