KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.proactive.core.config.ProActiveConfiguration;
34
35 /** The Applet : CruiseControlApplet
36  * models a car and a speed controller,
37  * and implements the primitives of the ProActive package.
38  *
39  *
40  * This class is responsible for the painting of the Car and Cruise Control Pane,
41  * recieve the user interaction on the 2 panes and send the corresponding messages to the Interface Dispatcher
42  *
43  */

44 public class CruiseControlApplet extends org.objectweb.proactive.examples.StandardFrame {
45
46   // components
47
/**
48    * the Pane associated with the Active Object CruiseControl
49    */

50   private CruiseControlPanel controlPane;
51   /**
52    * the Pane associated with the Active Object CarModel
53    */

54   private CarPanel carPane;
55   /**
56    * the active Object working as dispatcher between the Applet and the others Active Objects
57    */

58   private Interface activeObject;
59
60
61   public CruiseControlApplet(String JavaDoc name, int width, int height) {
62     super(name);
63     createActiveObject();
64     init(width, height);
65   }
66
67
68   public static void main(String JavaDoc arg[]) {
69     ProActiveConfiguration.load();
70     new CruiseControlApplet("Cruise Control", 840, 420);
71   }
72
73
74   // Methods
75

76   /**
77    * Initializes the Applet
78    * then creates the 3 objects Panels
79    *
80    * then creates the object Interface
81    */

82   public void createActiveObject() {
83     try {
84       activeObject = (Interface)org.objectweb.proactive.ProActive.turnActive(new Interface(this));
85     } catch (Exception JavaDoc e) {
86       e.printStackTrace();
87       System.exit(0);
88     }
89     activeObject.initialize();
90   }
91
92   protected void start(){}
93
94   protected javax.swing.JPanel JavaDoc createRootPanel() {
95     javax.swing.JPanel JavaDoc rootPanel = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(1, 1));
96     carPane = new CarPanel(this, activeObject);
97     controlPane = new CruiseControlPanel(this, activeObject);
98     javax.swing.JSplitPane JavaDoc horizontalSplitPane = new javax.swing.JSplitPane JavaDoc(javax.swing.JSplitPane.HORIZONTAL_SPLIT);
99     horizontalSplitPane.setDividerLocation(500);
100     horizontalSplitPane.setLeftComponent(carPane);
101     horizontalSplitPane.setRightComponent(controlPane);
102     rootPanel.add(horizontalSplitPane);
103     return rootPanel;
104   }
105
106
107   /**
108    * Calls the setDistance Method of the carPane
109    * and asks it to change the distance and to repaint it
110    */

111   public void setDistance(double distance) {
112     carPane.setDistance(1000 * distance);
113   }
114
115
116   /**
117    * Calls the setSpeed Method of the carPane
118    * to change the speed
119    */

120   public void setSpeed(double speed) {
121     carPane.setSpeed(speed);
122   }
123
124
125   /**
126    * Calls the setDesiredSpeed of the control pane,
127    * asks the applet to repaint it
128    */

129   public void setDesiredSpeed(double m_speed) {
130     controlPane.setDesiredSpeed(m_speed);
131     repaint();
132   }
133
134
135   /**
136    * Calls the engineOff Method of the carPane and the controlPane
137    */

138   public void engineOff() {
139     controlPane.controlOff();
140     carPane.engineOff();
141   }
142
143
144   public void engineOn() {
145     carPane.engineOn();
146   }
147
148   ////////////////////////////////////////////////////////////
149
/**
150    * Calls the setAlpha Method of the carPane to change the road incline
151    */

152   public void setAlpha(double m_alpha) {
153     carPane.setAlpha(m_alpha);
154   }
155
156
157   public void setAcceleration(double m_acc) {
158     carPane.setAcceleration(m_acc);
159   }
160
161
162   public void controlPaneOff() {
163     controlPane.controlOff();
164   }
165
166
167   public void brake() {
168     carPane.brake();
169   }
170
171
172   public void controlOn() {
173     activeObject.controlOn();
174     carPane.controlOn();
175     controlPane.controlOn();
176   }
177
178
179   public void controlOff() {
180     activeObject.controlOff();
181     carPane.controlOff();
182     controlPane.controlOff();
183   }
184 }
Popular Tags