1 26 27 package DiningPhilosophers.monolithic; 28 29 import DiningPhilosophers.*; 30 31 43 44 public class PhilosopherPanel 45 extends javax.swing.JPanel 46 { 47 48 54 58 protected javax.swing.JProgressBar hungryness_; 59 60 64 protected javax.swing.JCheckBox right_fork_; 65 66 70 protected javax.swing.JCheckBox left_fork_; 71 72 73 protected javax.swing.JLabel name_; 74 75 76 protected javax.swing.JLabel state_; 77 78 79 protected String [] stateAsStrings_ = { 80 "EATING", "THINKING", "HUNGRY", "STARVING", "DEAD" 81 }; 82 83 89 96 public PhilosopherPanel(StatusInfo status) 97 { 98 99 super(new java.awt.BorderLayout ()); 100 101 102 java.net.URL url; 103 javax.swing.JPanel hungrynessPanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 104 javax.swing.JPanel hungrynessSubPanel = new javax.swing.JPanel (new java.awt.FlowLayout ()); 105 106 107 java.awt.Font font = getFont(); 108 font = font.deriveFont((float)16.0); 109 110 112 hungryness_ = new javax.swing.JProgressBar (0, 40); 113 hungryness_.setSize(new java.awt.Dimension (150, 30)); 114 hungrynessSubPanel.add(hungryness_); 115 hungrynessPanel.add(new javax.swing.JLabel (" "), java.awt.BorderLayout.NORTH); 116 hungrynessPanel.add(hungrynessSubPanel, java.awt.BorderLayout.CENTER); 117 118 119 name_ = new javax.swing.JLabel (status.name); 120 name_.setPreferredSize(new java.awt.Dimension (100, name_.getHeight())); 121 name_.setFont(font); 122 123 124 right_fork_ = new javax.swing.JCheckBox (); 125 left_fork_ = new javax.swing.JCheckBox (); 126 127 128 state_ = new javax.swing.JLabel (); 129 state_.setPreferredSize(new java.awt.Dimension (100, state_.getHeight())); 130 state_.setFont(font); 131 132 133 url = PhilosopherPanel.class.getResource("/images/myfork.gif"); 134 right_fork_.setSelectedIcon(new javax.swing.ImageIcon (url)); 135 left_fork_.setSelectedIcon(new javax.swing.ImageIcon (url)); 136 url = PhilosopherPanel.class.getResource("/images/nofork.gif"); 137 right_fork_.setIcon(new javax.swing.ImageIcon (url)); 138 left_fork_.setIcon(new javax.swing.ImageIcon (url)); 139 140 141 add(name_, java.awt.BorderLayout.WEST); 142 143 144 javax.swing.JPanel forks = new javax.swing.JPanel (new java.awt.BorderLayout ()); 145 forks.add(right_fork_, java.awt.BorderLayout.EAST); 146 forks.add(state_, java.awt.BorderLayout.CENTER); 147 forks.add(left_fork_, java.awt.BorderLayout.WEST); 148 149 150 add(forks, java.awt.BorderLayout.CENTER); 151 152 add(hungrynessPanel, java.awt.BorderLayout.EAST); 153 154 155 updatePanel(status); 156 157 158 setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)); 159 setVisible(true); 160 } 161 162 168 174 protected java.awt.Color getFG(int thicks) 175 { 176 if (thicks < 3) return java.awt.Color.green; 177 if (thicks < 10) return java.awt.Color.yellow; 178 if (thicks < 40) return java.awt.Color.orange; 179 return java.awt.Color.red; 180 } 181 182 188 193 public void updatePanel(StatusInfo event) 194 { 195 196 state_.setText(stateAsStrings_[event.state.value()]); 197 198 hungryness_.setForeground(getFG(event.ticks_since_last_meal)); 199 hungryness_.setValue(event.ticks_since_last_meal); 200 201 left_fork_.setSelected(event.has_left_fork); 202 right_fork_.setSelected(event.has_right_fork); 203 } 204 } 205 206 | Popular Tags |