KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > cruisecontrol > CarPanel


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.cruisecontrol;
32
33 /** This class displays the speed, the acceleration of the car
34  * and holds the buttons for the interaction between the driver and the car
35  */

36 public class CarPanel extends javax.swing.JPanel JavaDoc {
37
38   // Fields
39
static final int INACTIVE = 0;
40   static final int ACTIVE = 1;
41   /** the current speed of the car */
42   double speed;
43   /** the distance to display */
44   double distance = 0;
45   /** the state of the car necessery for painting */
46   int state = INACTIVE;
47   /** The label accelerate the car */
48   javax.swing.JLabel JavaDoc Accelerate;
49   /** Acceleration bar */
50   javax.swing.JProgressBar JavaDoc accBar;
51   /** Road incline */
52   javax.swing.JProgressBar JavaDoc roadBar;
53   /** Label necessery to display the speed of the car */
54   javax.swing.JLabel JavaDoc speedLabel;
55   /** Label necessery to display the distance covering by the car */
56   javax.swing.JLabel JavaDoc distanceLabel;
57
58
59   /** Constructors : displays the buttons, the labels, the distance and the speedometer */
60
61   public CarPanel(final CruiseControlApplet parent, final Interface activeObject) {
62
63     setLayout(null);
64     setBorder(new javax.swing.border.LineBorder JavaDoc(java.awt.Color.black));
65       
66     ///////////////////////////////////////////////////////////
67
///////////////////////////////////////////////////////////
68
// Display
69
speedLabel = new javax.swing.JLabel JavaDoc("0");
70     distanceLabel = new javax.swing.JLabel JavaDoc("0");
71     javax.swing.JLabel JavaDoc speedDisplay = new javax.swing.JLabel JavaDoc("Speed");
72     javax.swing.JLabel JavaDoc distanceDisplay = new javax.swing.JLabel JavaDoc("Dist.");
73       
74       
75     //Buttons and Labels
76
javax.swing.JButton JavaDoc EngineOn = new javax.swing.JButton JavaDoc("Engine On");
77     javax.swing.JButton JavaDoc EngineOff = new javax.swing.JButton JavaDoc("Engine Off");
78     Accelerate = new javax.swing.JLabel JavaDoc("Accelerate");
79     javax.swing.JLabel JavaDoc Up = new javax.swing.JLabel JavaDoc("Up");
80     javax.swing.JLabel JavaDoc Down = new javax.swing.JLabel JavaDoc("Down");
81     javax.swing.JButton JavaDoc Brake = new javax.swing.JButton JavaDoc("Brake");
82     javax.swing.JButton JavaDoc Inc = new javax.swing.JButton JavaDoc("+");
83     javax.swing.JButton JavaDoc Dec = new javax.swing.JButton JavaDoc("-");
84     javax.swing.JButton JavaDoc UpAlpha = new javax.swing.JButton JavaDoc("Up");
85     javax.swing.JButton JavaDoc DownAlpha = new javax.swing.JButton JavaDoc("Down");
86
87     accBar = new javax.swing.JProgressBar JavaDoc(javax.swing.JProgressBar.VERTICAL, 0, 50);
88     accBar.setValue(0);
89     accBar.setStringPainted(true);
90
91     roadBar = new javax.swing.JProgressBar JavaDoc(javax.swing.JProgressBar.VERTICAL, -60, 60);
92     roadBar.setValue(0);
93     roadBar.setStringPainted(true);
94
95     EngineOn.setBounds(10, 310, 120, 30);
96     EngineOff.setBounds(140, 310, 130, 30);
97
98     Accelerate.setBounds(390, 310, 120, 30);
99     Up.setBounds(450, 10, 30, 30);
100     Down.setBounds(450, 120, 50, 30);
101     UpAlpha.setBounds(370, 20, 70, 30);
102     DownAlpha.setBounds(350, 110, 90, 30);
103
104     Brake.setBounds(280, 310, 80, 30);
105     Inc.setBounds(365, 345, 60, 30);
106     Dec.setBounds(430, 345, 60, 30);
107     accBar.setBounds(420, 200, 40, 80);
108     roadBar.setBounds(450, 40, 40, 80);
109
110     speedDisplay.setBounds(200, 230, 50, 30);
111     distanceDisplay.setBounds(200, 260, 50, 30);
112     speedLabel.setBounds(270, 230, 50, 30);
113     distanceLabel.setBounds(270, 260, 50, 30);
114       
115       
116     // add the components
117
add(EngineOn);
118     add(EngineOff);
119
120     add(Accelerate);
121     add(Up);
122     add(Down);
123
124     add(UpAlpha);
125     add(DownAlpha);
126
127     add(Brake);
128     add(Inc);
129     add(Dec);
130     add(accBar);
131     add(roadBar);
132
133     add(speedDisplay);
134     add(distanceDisplay);
135     add(speedLabel);
136     add(distanceLabel);
137
138     EngineOn.addActionListener(new java.awt.event.ActionListener JavaDoc() {
139
140       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
141         activeObject.engineOn();
142       }
143     });
144     EngineOff.addActionListener(new java.awt.event.ActionListener JavaDoc() {
145
146       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
147         activeObject.engineOff();
148       }
149     });
150     Brake.addActionListener(new java.awt.event.ActionListener JavaDoc() {
151
152       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
153         activeObject.brake();
154         brake();
155         parent.controlPaneOff();
156       }
157     });
158     Inc.addActionListener(new java.awt.event.ActionListener JavaDoc() {
159
160       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
161         activeObject.accelerate();
162       }
163     });
164     Dec.addActionListener(new java.awt.event.ActionListener JavaDoc() {
165
166       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
167         activeObject.decelerate();
168       }
169     });
170     UpAlpha.addActionListener(new java.awt.event.ActionListener JavaDoc() {
171
172       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
173         activeObject.incAlpha();
174       }
175     });
176     DownAlpha.addActionListener(new java.awt.event.ActionListener JavaDoc() {
177
178       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
179         activeObject.decAlpha();
180       }
181     });
182   }
183
184
185   ///////////////////////////////////////////////////////////////
186
//////////////////////////////////////////////////////////////
187

188   /** Turns the car to the acive state */
189   public void engineOn() {
190     if (this.state == INACTIVE) {
191       this.state = ACTIVE;
192       repaint();
193     }
194   }
195
196
197   /** Turns the car to the off state */
198   public void engineOff() {
199     setSpeed(0);
200     setAcceleration(0);
201     this.state = INACTIVE;
202     repaint();
203
204   }
205
206
207   /** Not used in this release */
208   public void controlOn() {
209     if (this.state == ACTIVE) {
210     }
211   }
212
213
214   /** Not used in this release */
215   public void controlOff() {
216   }
217
218
219   /** Not used in this release */
220   public void accelerate() {
221   }
222
223
224   /** Sets the current acceleration to null */
225   public void brake() {
226     setAcceleration(0);
227   }
228
229
230   ////////////////////////////////////////////////////////////////
231

232   /** Displays the car, the speedometer */
233   public void paintComponent(java.awt.Graphics JavaDoc g) {
234     super.paintComponent(g);
235     if (state == INACTIVE) {
236       g.setColor(java.awt.Color.black);
237       g.drawString("Engine Off", 40, 245);
238       g.setColor(java.awt.Color.red);
239       g.fillOval(110, 230, 20, 20);
240     } else {
241       g.setColor(java.awt.Color.black);
242       g.drawString("Engine On", 40, 245);
243       g.setColor(java.awt.Color.green);
244       g.fillOval(110, 230, 20, 20);
245     }
246
247     g.setColor(java.awt.Color.black);
248     g.drawOval(150, 40, 170, 170);
249
250     for (int i = 0; i <= 160; i += 10) {
251       drawMark(g, 150 + 85, 40 + 85, 85, i);
252     }
253     drawSpeed(g, 150 + 85, 40 + 85, 85);
254
255   }
256
257
258   /////////////////////////////////////////////////////////////
259
/////////////////////////////////////////////////////////////
260
/** called by paintComponent to display the speedometer */
261   private void drawSpeed(java.awt.Graphics JavaDoc g, int x, int y, int len) {
262     g.setColor(java.awt.Color.black);
263     double fangle = ((40 + (speed * 7) / 4) * Math.PI) / 180;
264
265     int mx = x - (int)((len - 30) * Math.sin(fangle));
266     int my = y + (int)((len - 30) * Math.cos(fangle));
267
268     g.drawLine(mx, my, x, y);
269   }
270
271
272   /** called by paintComponent to display the speedometer */
273   private void drawMark(java.awt.Graphics JavaDoc g, int x, int y, int len, int n) {
274     double flen = len;
275     double fangle = ((40 + n * 1.75) * Math.PI) / 180;
276     int mx = x - (int)(flen * Math.sin(fangle));
277     int my = y + (int)(flen * Math.cos(fangle));
278     int mx2 = x - (int)((flen - 5) * Math.sin(fangle));
279     int my2 = y + (int)((flen - 5) * Math.cos(fangle));
280
281     g.drawLine(mx, my, mx2, my2);
282
283     flen = flen + 12;
284     mx = x - 7 - (int)(flen * Math.sin(fangle));
285     my = y + 7 + (int)(flen * Math.cos(fangle));
286     g.drawString(String.valueOf(n), mx, my);
287
288   }
289
290
291   /** change the current speed to the new one, and displays it */
292   public void setSpeed(double m_speed) {
293     this.speed = m_speed;
294     this.speedLabel.setText(Integer.toString((int)speed));
295     repaint(150, 40, 170, 170);
296
297   }
298
299
300   /** change the current distance to the new one, and displays it */
301   public void setDistance(double m_distance) {
302     this.distance = m_distance;
303     this.distanceLabel.setText(Integer.toString((int)distance));
304
305   }
306
307
308   /** Displays the current acceleration in percentage */
309   public void setAcceleration(double m_acc) {
310     accBar.setValue((int)m_acc);
311   }
312
313
314   /** Displays the current road incline */
315   public void setAlpha(double m_alpha) {
316     roadBar.setValue((int)(m_alpha * 100));
317   }
318 }
Popular Tags