Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. /*
  2. * Simbad - Robot Simulator
  3. * Copyright (C) 2004 Louis Hugues
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. -----------------------------------------------------------------------------
  20. * $Author: sioulseuguh $
  21. * $Date: 2005/01/27 22:09:12 $
  22. * $Revision: 1.8 $
  23. * $Source: /cvsroot/simbad/src/simbad/gui/SimulatorControlGUI.java,v $
  24. */
  25. package simbad.gui;
  26.  
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.awt.event.KeyEvent;
  30. import java.awt.event.KeyListener;
  31.  
  32. import javax.swing.ButtonGroup;
  33. import javax.swing.JPanel;
  34. import java.awt.Container;
  35. import java.awt.Font;
  36. import java.util.ArrayList;
  37.  
  38. import javax.swing.BorderFactory;
  39. import javax.swing.BoxLayout;
  40. import javax.swing.JButton;
  41. import javax.swing.JLabel;
  42. import javax.swing.JFormattedTextField;
  43. import javax.swing.JRadioButton;
  44.  
  45. import simbad.sim.BallAgent;
  46. import simbad.sim.Simulator;
  47. import javax.swing.JFrame;
  48. import javax.swing.JMenu;
  49. import javax.vecmath.Vector3d;
  50.  
  51. import pong.MyRobot;
  52. import pong.Robarre;
  53.  
  54. /**
  55. * The GUI panel for controlling the simulator.
  56. */
  57. public class SimulatorControlGUI extends JPanel implements ActionListener, KeyListener {
  58.  
  59. private static final long serialVersionUID = 1L;
  60. Simulator simulator;
  61. JFormattedTextField timeFactorField;
  62. JFrame parentFrame;
  63. Font smallFont;
  64. ArrayList<MyRobot> joueur1;
  65. ArrayList<MyRobot> joueur2;
  66. BallAgent ballon;
  67.  
  68. public SimulatorControlGUI(JFrame parentFrame,Simulator simulator) {
  69. this.simulator = simulator;
  70.  
  71. this.joueur1 = new ArrayList<MyRobot>();
  72. this.joueur2 = new ArrayList<MyRobot>();
  73.  
  74. for (Object o : simulator.getAgentList()) {
  75. if (o instanceof MyRobot) {
  76. MyRobot r = (MyRobot)o;
  77. if (r.getName().contains("joueur1")) this.joueur1.add(r);
  78. if (r.getName().contains("joueur2")) this.joueur2.add(r);
  79. }
  80. if (o instanceof BallAgent) this.ballon = (BallAgent)o;
  81. }
  82.  
  83. smallFont = new Font("Arial",Font.PLAIN,11);
  84. setFont(smallFont);
  85. createGUI();
  86. this.parentFrame = parentFrame;
  87. }
  88.  
  89. public JMenu createMenu(){
  90. JMenu menu = new JMenu("Simulator");
  91. // TODO
  92. return menu;
  93. }
  94. void createGUI() {
  95. setBorder(BorderFactory.createCompoundBorder(
  96. BorderFactory.createTitledBorder("Simulator"),
  97. BorderFactory.createEmptyBorder(5,5,5,5)));
  98.  
  99. setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  100. // Control buttons
  101. JPanel panel1 = new JPanel();
  102. panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
  103. createButton(panel1, "run", "run");
  104. createButton(panel1, "pause", "pause");
  105. createButton(panel1, "reset", "reset");
  106. createButton(panel1, "restart", "restart");
  107. createButton(panel1, "step", "step1");
  108. add(panel1);
  109. // time factor buttons
  110. JPanel panel2 = new JPanel();
  111. panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
  112. createLabel(panel2,"Time Factor");
  113. ButtonGroup bgroup = new ButtonGroup();
  114. createRadioButton(panel2, bgroup, "0.2", "tf0.2",false);
  115. createRadioButton(panel2, bgroup, "0.5", "tf0.5",false);
  116. createRadioButton(panel2, bgroup, "1.0", "tf1.0",true);
  117. createRadioButton(panel2, bgroup, "5.0", "tf5.0",false);
  118. createRadioButton(panel2, bgroup, "10.0", "tf10.0",false);
  119. createRadioButton(panel2, bgroup, "20.0", "tf20.0",false);
  120.  
  121. add(panel2);
  122. }
  123.  
  124. /** helper function */
  125. private void createButton(Container container, String label, String action) {
  126. JButton b = new JButton(label);
  127. b.setFont(smallFont);
  128. b.setActionCommand(action);
  129. b.addActionListener(this);
  130. b.addKeyListener(this);
  131. container.add(b);
  132. }
  133.  
  134. /** helper function */
  135. private void createRadioButton(Container container, ButtonGroup group,
  136. String label, String action,boolean selected) {
  137. JRadioButton b = new JRadioButton(label);
  138. b.setActionCommand(action);
  139. b.setFont(smallFont);
  140. b.addActionListener(this);
  141. b.setSelected(selected);
  142. group.add(b);
  143. container.add(b);
  144. }
  145. /** helper function */
  146. private void createLabel(Container container, String label) {
  147. JLabel l = new JLabel(label);
  148. l.setFont(smallFont);
  149. container.add(l);
  150. }
  151.  
  152.  
  153. public void actionPerformed(ActionEvent actionEvent) {
  154. String action = actionEvent.getActionCommand();
  155. if (action.equals("run")) {
  156. simulator.startSimulation();
  157. } else if (action.equals("pause")) {
  158. simulator.stopSimulation();
  159. } else if (action.equals("reset")) {
  160. simulator.resetSimulation();
  161. } else if (action.equals("restart")) {
  162. simulator.restartSimulation();
  163. } else if (action.equals("tf0.2")) {
  164. simulator.setVirtualTimeFactor(0.2f);
  165. } else if (action.equals("tf0.5")) {
  166. simulator.setVirtualTimeFactor(0.5f);
  167. } else if (action.equals("tf1.0")) {
  168. simulator.setVirtualTimeFactor(1.0f);
  169. } else if (action.equals("tf2.0")) {
  170. simulator.setVirtualTimeFactor(2.0f);
  171. } else if (action.equals("tf5.0")) {
  172. simulator.setVirtualTimeFactor(5.0f);
  173. } else if (action.equals("tf10.0")) {
  174. simulator.setVirtualTimeFactor(10.0f);
  175. } else if (action.equals("tf20.0")) {
  176. simulator.setVirtualTimeFactor(20.0f);
  177. } else if (action.equals("tf50.0")) {
  178. simulator.setVirtualTimeFactor(50.0f);
  179. } else if (action.equals("step1")) {
  180. simulator.performSimulationStep();
  181.  
  182. }
  183.  
  184. }
  185.  
  186. @Override
  187. public void keyPressed(KeyEvent e) {
  188. if (e.getKeyCode() == KeyEvent.VK_Z) for (MyRobot r : this.joueur1) r.monter();
  189. if (e.getKeyCode() == KeyEvent.VK_S) for (MyRobot r : this.joueur1) r.descendre();
  190. if (e.getKeyCode() == KeyEvent.VK_UP) for (MyRobot r : this.joueur2) r.monter();
  191. if (e.getKeyCode() == KeyEvent.VK_DOWN) for (MyRobot r : this.joueur2) r.descendre();
  192. }
  193.  
  194. @Override
  195. public void keyReleased(KeyEvent e) {
  196. if (e.getKeyCode() == KeyEvent.VK_Z) for (MyRobot r : this.joueur1) r.stop();
  197. if (e.getKeyCode() == KeyEvent.VK_S) for (MyRobot r : this.joueur1) r.stop();
  198. if (e.getKeyCode() == KeyEvent.VK_UP) for (MyRobot r : this.joueur2) r.stop();
  199. if (e.getKeyCode() == KeyEvent.VK_DOWN) for (MyRobot r : this.joueur2) r.stop();
  200.  
  201. }
  202.  
  203. @Override
  204. public void keyTyped(KeyEvent e) {
  205. // TODO Auto-generated method stub
  206.  
  207. }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement