1 31 package org.objectweb.proactive.examples.doctor; 32 33 import java.awt.Dimension ; 34 import java.awt.Font ; 35 import java.awt.FontMetrics ; 36 import java.awt.Graphics ; 37 38 public class DisplayPanel extends javax.swing.JPanel implements Runnable { 39 40 Dimension prefSize; 41 int patState[], docState[]; 42 int nPat, nDoc; 43 public java.awt.Color wellOn,sickOn,cureOn,patColor, docColor; 44 Thread blinking; 45 boolean on; 46 47 48 public DisplayPanel() { 49 prefSize = new java.awt.Dimension (200, 200); 50 patState = new int[Office.MAX_PAT]; 51 docState = new int[Office.MAX_DOC]; 52 nPat = nDoc = 0; 53 54 wellOn = new java.awt.Color (0, 255, 0); 55 sickOn = new java.awt.Color (255, 0, 0); 56 cureOn = new java.awt.Color (0, 0, 255); 57 58 patColor = new java.awt.Color (0, 64, 0); 59 docColor = new java.awt.Color (0, 0, 64); 60 61 on = true; 62 blinking = new Thread (this); 63 blinking.start(); 64 } 65 66 67 public void run() { 68 while (true) { 69 try { 70 Thread.sleep(400); 71 } catch (InterruptedException e) { 72 } 73 on = (!on); 74 repaint(); 75 } 76 } 77 78 79 public Dimension getPreferredSize() { 80 Graphics g = getGraphics(); 81 82 FontMetrics fm = g.getFontMetrics(); 83 int wP = fm.stringWidth("Patient " + Office.MAX_PAT); 84 int wD = fm.stringWidth("Doctor " + Office.MAX_DOC); 85 int h = fm.getAscent(); 86 87 return new Dimension (130 + wP + wD + h, 80 + (h + 10) * (Office.NB_PAT - 1)); 88 } 89 90 91 public Dimension getMinimumSize() { 92 return new Dimension (100, 100); 93 } 94 95 96 protected void paintComponent(java.awt.Graphics g) { 97 super.paintComponent(g); 98 int i, w, h; 99 100 java.awt.Font oldF = g.getFont(); 101 java.awt.Dimension size = getSize(); 102 g.setFont(new Font ("Monospaced", Font.BOLD, 14)); 103 java.awt.FontMetrics fm = g.getFontMetrics(); 104 w = fm.stringWidth("The Doctor's Office"); 105 g.drawString("The Doctor's Office", (size.width - w) / 2, 20); 106 g.setFont(oldF); 107 108 java.awt.Color bg = getBackground(); 109 110 fm = g.getFontMetrics(); 111 w = fm.stringWidth("Patient " + Office.MAX_PAT); 112 h = fm.getAscent(); 113 114 for (i = 0; i < nDoc; i++) { 115 if (docState[i] != Office.DOC_UNDEF) { 116 int p = docState[i] - 1; 117 g.setColor(bg); 118 g.fillOval(60 + w, 50 + (h + 10) * p - h, h, h); 119 g.drawLine(60 + w + h, 50 + (h + 10) * p - (h / 2), 80 + w, 50 + (h + 10) * p - (h / 2)); 120 g.fillOval(80 + w, 50 + (h + 10) * p - h, h, h); 121 g.drawString("Doctor " + (i + 1), 90 + w + h, 50 + (h + 10) * p); 122 docState[i] = Office.DOC_UNDEF; 123 } 124 } 125 126 for (i = 0; i < nPat; i++) { 127 g.setColor(patColor); 128 g.drawString("Patient " + (i + 1), 50, 50 + (h + 10) * i); 129 130 switch (patState[i]) { 131 case Office.PAT_WELL: 132 case Office.PAT_SICK: 133 g.setColor((patState[i] == Office.PAT_WELL)?wellOn:sickOn); 134 g.fillOval(60 + w, 50 + (h + 10) * i - h, h, h); 135 break; 136 137 default: 138 g.setColor(on?cureOn:bg); 139 g.fillOval(60 + w, 50 + (h + 10) * i - h, h, h); 140 g.drawLine(60 + w + h, 50 + (h + 10) * i - (h / 2), 80 + w, 50 + (h + 10) * i - (h / 2)); 141 g.fillOval(80 + w, 50 + (h + 10) * i - h, h, h); 142 143 g.setColor(docColor); 144 g.drawString("Doctor " + patState[i], 90 + w + h, 50 + (h + 10) * i); 145 break; 146 } 147 } 148 } 149 150 151 public void setPatState(int pat, int state) { 152 patState[pat - 1] = state; 153 repaint(); 154 } 155 156 157 public void setDocFinished(int doc, int patient) { 158 docState[doc - 1] = patient; 159 repaint(); 160 } 161 162 163 public void addDoctor(int ps) { 164 docState[ps - 1] = Office.DOC_UNDEF; 165 nDoc++; 166 repaint(); 167 } 168 169 170 public void addPatient(int ps) { 171 patState[ps - 1] = Office.PAT_WELL; 172 nPat++; 173 repaint(); 174 } 175 } 176 177 | Popular Tags |