/src/Examples/org/objectweb/proactive/examples/cruisecontrol/CruiseControlApplet.java

https://bitbucket.org/lp/programming-multiactivities · Java · 186 lines · 81 code · 24 blank · 81 comment · 0 complexity · b50b77659e6228ce1862031f38071995 MD5 · raw file

  1. /*
  2. * ################################################################
  3. *
  4. * ProActive Parallel Suite(TM): The Java(TM) library for
  5. * Parallel, Distributed, Multi-Core Computing for
  6. * Enterprise Grids & Clouds
  7. *
  8. * Copyright (C) 1997-2012 INRIA/University of
  9. * Nice-Sophia Antipolis/ActiveEon
  10. * Contact: proactive@ow2.org or contact@activeeon.com
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Affero General Public License
  14. * as published by the Free Software Foundation; version 3 of
  15. * the License.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  25. * USA
  26. *
  27. * If needed, contact us to obtain a release under GPL Version 2 or 3
  28. * or a different license than the AGPL.
  29. *
  30. * Initial developer(s): The ProActive Team
  31. * http://proactive.inria.fr/team_members.htm
  32. * Contributor(s):
  33. *
  34. * ################################################################
  35. * $$PROACTIVE_INITIAL_DEV$$
  36. */
  37. package org.objectweb.proactive.examples.cruisecontrol;
  38. import org.objectweb.proactive.core.config.ProActiveConfiguration;
  39. /** The Applet : CruiseControlApplet
  40. * models a car and a speed controller,
  41. * and implements the primitives of the ProActive package.
  42. *
  43. *
  44. * This class is responsible for the painting of the Car and Cruise Control Pane,
  45. * recieve the user interaction on the 2 panes and send the corresponding messages to the Interface Dispatcher
  46. *
  47. */
  48. public class CruiseControlApplet extends org.objectweb.proactive.examples.StandardFrame {
  49. // components
  50. /**
  51. * the Pane associated with the Active Object CruiseControl
  52. */
  53. private CruiseControlPanel controlPane;
  54. /**
  55. * the Pane associated with the Active Object CarModel
  56. */
  57. private CarPanel carPane;
  58. /**
  59. * the active Object working as dispatcher between the Applet and the others Active Objects
  60. */
  61. private Interface activeObject;
  62. public CruiseControlApplet(String name, int width, int height) {
  63. super(name);
  64. createActiveObject();
  65. init(width, height);
  66. }
  67. public static void main(String[] arg) {
  68. ProActiveConfiguration.load();
  69. new CruiseControlApplet("Cruise Control", 840, 420);
  70. }
  71. // Methods
  72. /**
  73. * Initializes the Applet
  74. * then creates the 3 objects Panels
  75. *
  76. * then creates the object Interface
  77. */
  78. public void createActiveObject() {
  79. try {
  80. activeObject = (Interface) org.objectweb.proactive.api.PAActiveObject.turnActive(new Interface(
  81. this));
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. System.exit(0);
  85. }
  86. activeObject.initialize();
  87. }
  88. @Override
  89. protected void start() {
  90. }
  91. @Override
  92. protected javax.swing.JPanel createRootPanel() {
  93. javax.swing.JPanel rootPanel = new javax.swing.JPanel(new java.awt.GridLayout(1, 1));
  94. carPane = new CarPanel(this, activeObject);
  95. controlPane = new CruiseControlPanel(this, activeObject);
  96. javax.swing.JSplitPane horizontalSplitPane = new javax.swing.JSplitPane(
  97. javax.swing.JSplitPane.HORIZONTAL_SPLIT);
  98. horizontalSplitPane.setDividerLocation(500);
  99. horizontalSplitPane.setLeftComponent(carPane);
  100. horizontalSplitPane.setRightComponent(controlPane);
  101. rootPanel.add(horizontalSplitPane);
  102. return rootPanel;
  103. }
  104. /**
  105. * Calls the setDistance Method of the carPane
  106. * and asks it to change the distance and to repaint it
  107. */
  108. public void setDistance(double distance) {
  109. carPane.setDistance(1000 * distance);
  110. }
  111. /**
  112. * Calls the setSpeed Method of the carPane
  113. * to change the speed
  114. */
  115. public void setSpeed(double speed) {
  116. carPane.setSpeed(speed);
  117. }
  118. /**
  119. * Calls the setDesiredSpeed of the control pane,
  120. * asks the applet to repaint it
  121. */
  122. public void setDesiredSpeed(double m_speed) {
  123. controlPane.setDesiredSpeed(m_speed);
  124. repaint();
  125. }
  126. /**
  127. * Calls the engineOff Method of the carPane and the controlPane
  128. */
  129. public void engineOff() {
  130. controlPane.controlOff();
  131. carPane.engineOff();
  132. }
  133. public void engineOn() {
  134. carPane.engineOn();
  135. }
  136. ////////////////////////////////////////////////////////////
  137. /**
  138. * Calls the setAlpha Method of the carPane to change the road incline
  139. */
  140. public void setAlpha(double m_alpha) {
  141. carPane.setAlpha(m_alpha);
  142. }
  143. public void setAcceleration(double m_acc) {
  144. carPane.setAcceleration(m_acc);
  145. }
  146. public void controlPaneOff() {
  147. controlPane.controlOff();
  148. }
  149. public void brake() {
  150. carPane.brake();
  151. }
  152. public void controlOn() {
  153. activeObject.controlOn();
  154. carPane.controlOn();
  155. controlPane.controlOn();
  156. }
  157. public void controlOff() {
  158. activeObject.controlOff();
  159. carPane.controlOff();
  160. controlPane.controlOff();
  161. }
  162. }