1 31 package org.objectweb.proactive.examples.boundedbuffer; 32 33 import org.objectweb.proactive.core.config.ProActiveConfiguration; 34 35 47 public class AppletBuffer extends org.objectweb.proactive.examples.StandardFrame { 48 49 52 public CellPanel cells[]; 53 private int max; 54 private ActiveDisplay display; 55 private javax.swing.JButton bProd; 56 private javax.swing.JButton bCons; 57 58 59 public AppletBuffer(String name, int width, int height) { 60 super(name); 61 max = 7; 62 init(width, height); 63 } 64 65 66 public static void main(String args[]) { 67 ProActiveConfiguration.load(); 68 new AppletBuffer("Bounded Buffer", 500, 300); 69 } 70 71 72 public void start() { 73 cells[0].setIn(true); 74 cells[0].setOut(true); 75 try { 76 display = (ActiveDisplay)org.objectweb.proactive.ProActive.turnActive(new ActiveDisplay(max, this)); 77 receiveMessage("ActiveDisplay created : " + display.getClass().getName()); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } 81 display.start(); 82 } 83 84 85 public void kill() { 86 display.done(); 87 } 88 89 90 public void setCell(int pos, String str) { 91 cells[pos].setCaption(str); 93 repaint(); 94 } 95 96 97 public void setOut(int pos, boolean val) { 98 cells[pos].setOut(val); 100 repaint(); 101 } 102 103 104 public void setIn(int pos, boolean val) { 105 cells[pos].setIn(val); 107 repaint(); 108 } 109 110 111 public void consumerStartRunning() { 112 receiveMessage("consumer now start running"); 113 bCons.setText("Stop"); 114 } 115 116 117 public void consumerStopRunning() { 118 receiveMessage("consumer now stop running"); 119 bCons.setText("Start"); 120 } 121 122 123 public void producerStartRunning() { 124 receiveMessage("producer now start running"); 125 bProd.setText("Stop"); 126 } 127 128 129 public void producerStopRunning() { 130 receiveMessage("producer now stop running"); 131 bProd.setText("Start"); 132 } 133 134 138 protected javax.swing.JPanel createRootPanel() { 139 javax.swing.JPanel rootPanel = new javax.swing.JPanel (); 140 rootPanel.setBackground(java.awt.Color.white); 141 rootPanel.setLayout(new java.awt.GridLayout (3, 1)); 142 143 javax.swing.JPanel panel = new javax.swing.JPanel (); 145 javax.swing.JLabel label = new javax.swing.JLabel ("Producer"); 146 label.setForeground(java.awt.Color.red); 147 panel.add(label); 148 bProd = new javax.swing.JButton ("Start"); 149 bProd.setBackground(java.awt.Color.lightGray); 150 bProd.addActionListener(new java.awt.event.ActionListener () { 151 152 public void actionPerformed(java.awt.event.ActionEvent e) { 153 display.toggleProd(); 154 } 155 }); 156 panel.add(bProd); 157 158 rootPanel.add(panel); 159 160 javax.swing.JPanel cellsPanel = new javax.swing.JPanel (); 162 cells = new CellPanel[max]; 163 for (int i = 0; i < max; i++) { 164 cells[i] = new CellPanel(); 165 cellsPanel.add(cells[i]); 166 } 167 rootPanel.add(cellsPanel); 168 169 panel = new javax.swing.JPanel (); 171 label = new javax.swing.JLabel ("Consumer"); 172 label.setForeground(java.awt.Color.green); 173 panel.add(label); 174 bCons = new javax.swing.JButton ("Start"); 175 bCons.setBackground(java.awt.Color.lightGray); 176 bCons.addActionListener(new java.awt.event.ActionListener () { 177 178 public void actionPerformed(java.awt.event.ActionEvent e) { 179 display.toggleCons(); 180 } 181 }); 182 panel.add(bCons); 183 rootPanel.add(panel); 184 return rootPanel; 185 } 186 187 188 192 protected class CellPanel extends javax.swing.JPanel { 193 194 private javax.swing.JLabel caption; 195 private boolean in; 196 private boolean out; 197 private boolean empty; 198 199 200 public CellPanel() { 201 super(); 202 in = out = false; 203 empty = true; 204 caption = new javax.swing.JLabel ("-empty-"); 205 add(caption); 206 java.awt.Dimension d = new java.awt.Dimension (80, 25); 207 this.setSize(d); 208 this.setPreferredSize(d); 209 this.setMinimumSize(d); 210 this.setMaximumSize(d); 211 } 212 213 214 public void setIn(boolean in) { 215 this.in = in; 216 repaint(); 217 } 218 219 220 public void setOut(boolean out) { 221 this.out = out; 222 repaint(); 223 } 224 225 226 public void setCaption(String str) { 227 if (str == null) { 228 str = "-empty-"; 229 empty = true; 230 } else { 231 empty = false; 232 } 233 caption.setText(str); 234 } 235 236 237 protected void paintComponent(java.awt.Graphics g) { 238 super.paintComponent(g); 239 if (empty) 240 setBackground(java.awt.Color.gray); 241 else 242 setBackground(java.awt.Color.blue); 243 if (in) { 244 java.awt.Color old = g.getColor(); 245 g.setColor(java.awt.Color.red); 246 g.fillRect(1, 1, 5, 5); 247 g.setColor(old); 248 } 249 if (out) { 250 java.awt.Color old = g.getColor(); 251 g.setColor(java.awt.Color.green); 252 g.fillRect(1, 10, 5, 5); 253 g.setColor(old); 254 } 255 } 256 } 257 } | Popular Tags |