KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > doctor > DisplayPanel


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.doctor;
32
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Font JavaDoc;
35 import java.awt.FontMetrics JavaDoc;
36 import java.awt.Graphics JavaDoc;
37
38 public class DisplayPanel extends javax.swing.JPanel JavaDoc implements Runnable JavaDoc {
39
40   Dimension JavaDoc prefSize;
41   int patState[], docState[];
42   int nPat, nDoc;
43   public java.awt.Color JavaDoc wellOn,sickOn,cureOn,patColor, docColor;
44   Thread JavaDoc blinking;
45   boolean on;
46
47
48   public DisplayPanel() {
49     prefSize = new java.awt.Dimension JavaDoc(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 JavaDoc(0, 255, 0);
55     sickOn = new java.awt.Color JavaDoc(255, 0, 0);
56     cureOn = new java.awt.Color JavaDoc(0, 0, 255);
57
58     patColor = new java.awt.Color JavaDoc(0, 64, 0);
59     docColor = new java.awt.Color JavaDoc(0, 0, 64);
60
61     on = true;
62     blinking = new Thread JavaDoc(this);
63     blinking.start();
64   }
65
66
67   public void run() {
68     while (true) {
69       try {
70         Thread.sleep(400);
71       } catch (InterruptedException JavaDoc e) {
72       }
73       on = (!on);
74       repaint();
75     }
76   }
77
78
79   public Dimension JavaDoc getPreferredSize() {
80     Graphics JavaDoc g = getGraphics();
81
82     FontMetrics JavaDoc 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 JavaDoc(130 + wP + wD + h, 80 + (h + 10) * (Office.NB_PAT - 1));
88   }
89
90
91   public Dimension JavaDoc getMinimumSize() {
92     return new Dimension JavaDoc(100, 100);
93   }
94
95
96   protected void paintComponent(java.awt.Graphics JavaDoc g) {
97     super.paintComponent(g);
98     int i, w, h;
99
100     java.awt.Font JavaDoc oldF = g.getFont();
101     java.awt.Dimension JavaDoc size = getSize();
102     g.setFont(new Font JavaDoc("Monospaced", Font.BOLD, 14));
103     java.awt.FontMetrics JavaDoc 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 JavaDoc 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