KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.swing.JButton JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35
36 /** Screen of the controller,
37  * displays the desired speed.
38  * 4 buttons allow to turn on or off the controller
39  * and to change the desired speed
40  */

41 public class CruiseControlPanel extends JPanel JavaDoc {
42
43   // Fields
44

45   /** constants : represents the controller on */
46   final static int ACTIVE = 1;
47   final static int INACTIVE = 0;
48   /** Turns off the controller */
49   JButton JavaDoc CruiseOff;
50   /** Turns on the controller */
51   JButton JavaDoc CruiseOn;
52   /** Increases the desired speed */
53   JButton JavaDoc Inc1;
54   /** Decreases the desired speed */
55   JButton JavaDoc Dec1;
56   /** the desired speed of the active controller */
57   int desiredSpeed = 0;
58   /** java.awt.Font used to display some components */
59   java.awt.Font JavaDoc big = new java.awt.Font JavaDoc("SansSerif", java.awt.Font.BOLD, 18);
60   /** java.awt.Font used to display some components */
61   java.awt.Font JavaDoc small = new java.awt.Font JavaDoc("SansSerif", java.awt.Font.PLAIN, 14);
62   /** state of the car necessery for the painting of the car */
63   int state = INACTIVE;
64
65
66   /** Constructor which initializes the labels, the buttons, and places each components of the car */
67   public CruiseControlPanel(final CruiseControlApplet parent, final Interface activeObject) {
68     setLayout(null);
69     //setBorder(new LineBorder(Color.black));
70

71     CruiseOff = new JButton JavaDoc("Control Off");
72     CruiseOn = new JButton JavaDoc("Control On");
73     Inc1 = new JButton JavaDoc("++");
74     Dec1 = new JButton JavaDoc("--");
75
76     CruiseOn.setBounds(20, 310, 130, 30);
77     CruiseOff.setBounds(160, 310, 130, 30);
78     Inc1.setBounds(90, 345, 60, 30);
79     Dec1.setBounds(160, 345, 60, 30);
80
81     add(CruiseOn);
82     add(CruiseOff);
83     add(Inc1);
84     add(Dec1);
85
86     CruiseOn.addActionListener(new java.awt.event.ActionListener JavaDoc() {
87
88       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
89         parent.controlOn();
90       }
91     });
92     CruiseOff.addActionListener(new java.awt.event.ActionListener JavaDoc() {
93
94       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
95         parent.controlOff();
96       }
97     });
98     Inc1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
99
100       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
101         activeObject.accelerateCruise();
102       }
103     });
104     Dec1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
105
106       public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
107         activeObject.decelerateCruise();
108       }
109     });
110   }
111
112   // Methods
113

114   /** Displays the desired speed and of the state of the controller */
115   public void paintComponent(java.awt.Graphics JavaDoc g) {
116     super.paintComponent(g);
117
118     g.setColor(java.awt.Color.black);
119     g.setFont(big);
120     g.drawString("Control", 100, 40);
121     g.setColor(java.awt.Color.black);
122
123     g.setFont(small);
124     g.drawString("Desired Speed ", 100, 120);
125     g.drawRect(110, 140, 70, 20);
126     // display the desired speed
127
g.drawString(String.valueOf(desiredSpeed), 120, 155);
128       
129     // display the state off the controller
130

131     if (state == INACTIVE) {
132       g.setColor(java.awt.Color.black);
133       g.drawString("Controller Off", 80, 230);
134       g.setColor(java.awt.Color.red);
135       g.fillOval(180, 210, 30, 30);
136     } else {
137       g.setColor(java.awt.Color.black);
138       g.drawString("Controller On", 80, 230);
139       g.setColor(java.awt.Color.green);
140       g.fillOval(180, 210, 30, 30);
141     }
142   }
143
144   ///////////////////////////////////////////////////////////////
145

146   /** Activates the controller */
147   public void controlOn() {
148     if (this.state == INACTIVE) {
149       this.state = ACTIVE;
150       repaint();
151     }
152   }
153
154
155   /** Deactivates the controller */
156   public void controlOff() {
157     if (this.state == ACTIVE) {
158       this.state = INACTIVE;
159       repaint();
160     }
161   }
162
163
164   /** set the desired speed to the new desired speed */
165   public void setDesiredSpeed(double m_speed) {
166     desiredSpeed = (int)m_speed;
167
168   }
169 }
Popular Tags