Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. package se.chalmers.FSA.vehicle;
  2.  
  3. import se.chalmers.FSA.vehicle.Model.CarModel;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.event.ChangeEvent;
  7. import javax.swing.event.ChangeListener;
  8. import java.awt.*;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11.  
  12. /**
  13. * This class represents the full view of the MVC pattern of your car simulator.
  14. * It initializes with being center on the screen and attaching it's controller in it's state.
  15. * It communicates with the Controller by calling methods of it when an action fires of in
  16. * each of it's components.
  17. * TODO: Write more actionListeners and wire the rest of the buttons
  18. **/
  19.  
  20. public class CarView extends JFrame {
  21. private static final int X = 800;
  22. private static final int Y = 800;
  23.  
  24. // The controller member
  25. DrawPanel drawPanel;
  26. DrawCarInfo drawCarInfo;
  27.  
  28. JPanel controlPanel = new JPanel();
  29.  
  30. JPanel gasPanel = new JPanel();
  31.  
  32. String[] carChoices = {"Random","Saab95","Volvo240","Scania"};
  33. JComboBox<String> carComboBox = new JComboBox<>(carChoices);
  34.  
  35. int gasAmount = 0;
  36. int brakeAmount= 0;
  37.  
  38. int carXCordinate = 0;
  39. int carYCordinate = 0;
  40.  
  41. JSpinner xCoordinateSpinner;
  42. JSpinner yCordinateSpinner;
  43. JLabel xCordinateSpinnerLabel = new JLabel("Car starting X cordinate");
  44.  
  45.  
  46.  
  47. JSpinner gasSpinner;
  48. JLabel gasSpinnerLabel = new JLabel("Gas/Brake", SwingConstants.CENTER);
  49.  
  50. JButton gasButton = new JButton("Gas");
  51. JButton brakeButton = new JButton("Brake");
  52. JButton turboOnButton = new JButton("Turbo on");
  53. JButton turboOffButton = new JButton("Turbo off");
  54. JButton raiseBedButton = new JButton("Raise truck bed");
  55. JButton lowerBedButton = new JButton("Lower truck bed");
  56. JButton addCarButton = new JButton("Add a car");
  57. JButton removeCarButton = new JButton("Removes a car");
  58.  
  59. JButton startButton = new JButton("Start all cars");
  60. JButton stopButton = new JButton("Stop all cars");
  61.  
  62. // Constructor
  63. public CarView(String framename,CarController carC, CarModel carModel){
  64. this.drawPanel = new DrawPanel(X, Y-240, carModel);
  65. initComponents(framename,carC,carModel);
  66. }
  67.  
  68.  
  69.  
  70.  
  71. // Sets everything in place and fits everything
  72. // TODO: Take a good look and make sure you understand how these methods and components work
  73. private void initComponents(String title, CarController carC, CarModel carModel) {
  74.  
  75. this.setTitle(title);
  76. this.setPreferredSize(new Dimension(X,Y));
  77. this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
  78.  
  79. this.add(drawPanel);
  80.  
  81. SpinnerModel gasSpinnerModel =
  82. new SpinnerNumberModel(0, //initial value
  83. 0, //min
  84. 100, //max
  85. 1);//step
  86. gasSpinner = new JSpinner(gasSpinnerModel);
  87. gasSpinner.addChangeListener(new ChangeListener() {
  88. public void stateChanged(ChangeEvent e) {
  89. gasAmount = (int) ((JSpinner)e.getSource()).getValue();
  90. brakeAmount = (int) ((JSpinner)e.getSource()).getValue();
  91. }
  92. });
  93.  
  94. SpinnerModel xCordinateSpinnerModel =
  95. new SpinnerNumberModel(0, //initial value
  96. 0, //min
  97. 800, //max
  98. 1);//step
  99. xCoordinateSpinner = new JSpinner(xCordinateSpinnerModel);
  100. xCoordinateSpinner.addChangeListener(new ChangeListener() {
  101. public void stateChanged(ChangeEvent e) {
  102. carXCordinate = (int) ((JSpinner)e.getSource()).getValue();
  103. }
  104. });
  105.  
  106. gasPanel.setLayout(new GridLayout(2,1));
  107. gasPanel.add(gasSpinnerLabel, 0);
  108. gasPanel.add(gasSpinner, 1);
  109. gasPanel.setPreferredSize(new Dimension((X/10), 200));
  110. gasPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
  111. gasPanel.setBackground(Color.CYAN);
  112.  
  113. this.add(gasPanel);
  114.  
  115. controlPanel.setLayout(new GridLayout(2,5));
  116.  
  117. controlPanel.add(gasButton, 0);
  118. controlPanel.add(turboOnButton, 1);
  119. controlPanel.add(raiseBedButton, 2);
  120. controlPanel.add(brakeButton, 3);
  121. controlPanel.add(turboOffButton, 4);
  122. controlPanel.add(lowerBedButton, 5);
  123. controlPanel.add(addCarButton,6);
  124. controlPanel.add(removeCarButton, 7);
  125. controlPanel.add(carComboBox, 8);
  126. controlPanel.add(xCoordinateSpinner, 9);
  127. controlPanel.setPreferredSize(new Dimension((X/2)+75, 200));
  128. this.add(controlPanel);
  129. controlPanel.setBackground(Color.CYAN);
  130.  
  131.  
  132. startButton.setBackground(Color.blue);
  133. startButton.setForeground(Color.green);
  134. startButton.setPreferredSize(new Dimension(X/7,200));
  135. this.add(startButton);
  136.  
  137.  
  138. stopButton.setBackground(Color.red);
  139. stopButton.setForeground(Color.black);
  140. stopButton.setPreferredSize(new Dimension(X/7,200));
  141. this.add(stopButton);
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. // This actionListener is for the gas button only
  150. // TODO: Create more for each component as necessary
  151. gasButton.addActionListener(new ActionListener() {
  152. @Override
  153. public void actionPerformed(ActionEvent e) {
  154. carC.gas(gasAmount);
  155. }
  156. });
  157.  
  158. stopButton.addActionListener(new ActionListener() {
  159. @Override
  160. public void actionPerformed(ActionEvent e) {
  161. carC.stopEngine();
  162. }
  163. });
  164. startButton.addActionListener(new ActionListener() {
  165. @Override
  166. public void actionPerformed(ActionEvent e) {
  167. carC.startEngine();
  168. }
  169. });
  170. brakeButton.addActionListener(new ActionListener() {
  171. @Override
  172. public void actionPerformed(ActionEvent e) {
  173. carC.brake(brakeAmount);
  174. }
  175. });
  176. turboOnButton.addActionListener(new ActionListener() {
  177. @Override
  178. public void actionPerformed(ActionEvent e) {
  179. carC.turboOn();
  180. }
  181. });
  182. turboOffButton.addActionListener(new ActionListener() {
  183. @Override
  184. public void actionPerformed(ActionEvent e) {
  185. carC.turboOff();
  186. }
  187. });
  188. raiseBedButton.addActionListener(new ActionListener() {
  189. @Override
  190. public void actionPerformed(ActionEvent e) {
  191. carC.raiseTruckBed();
  192. }
  193. });
  194. lowerBedButton.addActionListener(new ActionListener() {
  195. @Override
  196. public void actionPerformed(ActionEvent e) {
  197. carC.lowerTruckBed();
  198. }
  199. });
  200. addCarButton.addActionListener(new ActionListener() {
  201. @Override
  202. public void actionPerformed(ActionEvent e) {
  203. switch (carComboBox.getItemAt(carComboBox.getSelectedIndex())){
  204. case "Saab95":
  205. carC.addNewSaab95(carXCordinate,0);
  206. break;
  207. case "Volvo240":
  208. carC.addNewVolvo240(carXCordinate,0);
  209. break;
  210. case "Scania":
  211. carC.addNewScania(carXCordinate,0);
  212. break;
  213. case "Random":
  214. carC.addRandomCar(carXCordinate,0);
  215. }
  216. }
  217. });
  218.  
  219. removeCarButton.addActionListener(new ActionListener() {
  220. @Override
  221. public void actionPerformed(ActionEvent e) {
  222. carC.removeCar();
  223. }
  224. });
  225.  
  226. // Make the frame pack all it's components by respecting the sizes if possible.
  227. this.pack();
  228.  
  229. // Get the computer screen resolution
  230. Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  231. // Center the frame
  232. this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
  233. // Make the frame visible
  234. this.setVisible(true);
  235. // Make sure the frame exits when "x" is pressed
  236. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  237.  
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement