1 26 27 package DiningPhilosophers.cif; 28 29 import DiningPhilosophers.*; 30 31 42 43 public class PhilosopherPdaPanel 44 extends java.awt.Panel 45 { 46 47 53 57 59 63 protected MyCheckBox right_fork_; 64 65 69 protected MyCheckBox left_fork_; 70 71 72 protected java.awt.Label name_; 73 74 75 protected java.awt.Label state_; 76 77 78 protected String [] stateAsStrings_ = { 79 "EATING", "THINKING", "HUNGRY", "STARVING", "DEAD" 80 }; 81 82 88 95 public PhilosopherPdaPanel(StatusInfo status) 96 { 97 98 super(new java.awt.FlowLayout (java.awt.FlowLayout.LEFT)); 99 100 101 java.net.URL url; 102 java.awt.Image my_fork, no_fork; 103 104 106 112 113 name_ = new java.awt.Label (status.name); 114 name_.setSize(new java.awt.Dimension (100, name_.getHeight())); 115 116 117 right_fork_ = new MyCheckBox(); 118 left_fork_ = new MyCheckBox(); 119 120 121 state_ = new java.awt.Label (); 122 state_.setSize(new java.awt.Dimension (100, state_.getHeight())); 123 124 125 url = PhilosopherPdaPanel.class.getResource("/images/myfork.gif"); 126 my_fork = java.awt.Toolkit.getDefaultToolkit().getImage(url); 127 right_fork_.setSelectedIcon(my_fork); 128 left_fork_.setSelectedIcon(my_fork); 129 url = PhilosopherPdaPanel.class.getResource("/images/nofork.gif"); 130 no_fork = java.awt.Toolkit.getDefaultToolkit().getImage(url); 131 right_fork_.setIcon(no_fork); 132 left_fork_.setIcon(no_fork); 133 right_fork_.setSelected(false); 134 left_fork_.setSelected(false); 135 right_fork_.setSize(50, 50); 136 left_fork_.setSize(50, 50); 137 138 139 140 add(name_); 141 142 143 java.awt.Panel forks = new java.awt.Panel (new java.awt.FlowLayout ()); 144 add(right_fork_); 145 add(state_); 146 add(left_fork_); 147 148 149 151 152 updatePanel(status); 153 validate(); 154 155 156 setVisible(true); 157 } 158 159 165 171 176 public void updatePanel(StatusInfo event) 177 { 178 179 state_.setText(stateAsStrings_[event.state.value()]); 180 181 184 left_fork_.setSelected(event.has_left_fork); 185 right_fork_.setSelected(event.has_right_fork); 186 } 187 } 188 189 192 class MyCheckBox extends java.awt.Panel 193 { 194 195 196 protected java.awt.Image selectedIcon_, icon_; 197 198 199 protected boolean selected_; 200 201 204 public MyCheckBox() { 205 selected_ = false; 206 }; 207 208 211 public void setSelectedIcon(java.awt.Image icon) 212 { 213 selectedIcon_ = icon; 214 } 215 216 219 public void setIcon(java.awt.Image icon) 220 { 221 icon_ = icon; 222 } 223 224 227 public void setSelected(boolean b) 228 { 229 selected_ = b; 230 repaint(); 231 } 232 233 237 public java.awt.Dimension getPreferredSize() 238 { 239 return new java.awt.Dimension (50, 50); 240 } 241 242 246 public void paint(java.awt.Graphics g) 247 { 248 if (selected_){ 249 g.drawImage(selectedIcon_, 0, 0, this); 250 } else { 251 g.drawImage(icon_, 0, 0, this); 252 } 253 } 254 } 255 | Popular Tags |