-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathCarView.java
123 lines (97 loc) · 4.32 KB
/
CarView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* This class represents the full view of the MVC pattern of your car simulator.
* It initializes with being center on the screen and attaching it's controller in it's state.
* It communicates with the Controller by calling methods of it when an action fires of in
* each of it's components.
* TODO: Write more actionListeners and wire the rest of the buttons
**/
public class CarView extends JFrame{
private static final int X = 800;
private static final int Y = 800;
// The controller member
CarController carC;
DrawPanel drawPanel = new DrawPanel(X, Y-240);
JPanel controlPanel = new JPanel();
JPanel gasPanel = new JPanel();
JSpinner gasSpinner = new JSpinner();
int gasAmount = 0;
JLabel gasLabel = new JLabel("Amount of gas");
JButton gasButton = new JButton("Gas");
JButton brakeButton = new JButton("Brake");
JButton turboOnButton = new JButton("Saab Turbo on");
JButton turboOffButton = new JButton("Saab Turbo off");
JButton liftBedButton = new JButton("Scania Lift Bed");
JButton lowerBedButton = new JButton("Lower Lift Bed");
JButton startButton = new JButton("Start all cars");
JButton stopButton = new JButton("Stop all cars");
// Constructor
public CarView(String framename, CarController cc){
this.carC = cc;
initComponents(framename);
}
// Sets everything in place and fits everything
// TODO: Take a good look and make sure you understand how these methods and components work
private void initComponents(String title) {
this.setTitle(title);
this.setPreferredSize(new Dimension(X,Y));
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
this.add(drawPanel);
SpinnerModel spinnerModel =
new SpinnerNumberModel(0, //initial value
0, //min
100, //max
1);//step
gasSpinner = new JSpinner(spinnerModel);
gasSpinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
gasAmount = (int) ((JSpinner)e.getSource()).getValue();
}
});
gasPanel.setLayout(new BorderLayout());
gasPanel.add(gasLabel, BorderLayout.PAGE_START);
gasPanel.add(gasSpinner, BorderLayout.PAGE_END);
this.add(gasPanel);
controlPanel.setLayout(new GridLayout(2,4));
controlPanel.add(gasButton, 0);
controlPanel.add(turboOnButton, 1);
controlPanel.add(liftBedButton, 2);
controlPanel.add(brakeButton, 3);
controlPanel.add(turboOffButton, 4);
controlPanel.add(lowerBedButton, 5);
controlPanel.setPreferredSize(new Dimension((X/2)+4, 200));
this.add(controlPanel);
controlPanel.setBackground(Color.CYAN);
startButton.setBackground(Color.blue);
startButton.setForeground(Color.green);
startButton.setPreferredSize(new Dimension(X/5-15,200));
this.add(startButton);
stopButton.setBackground(Color.red);
stopButton.setForeground(Color.black);
stopButton.setPreferredSize(new Dimension(X/5-15,200));
this.add(stopButton);
// This actionListener is for the gas button only
// TODO: Create more for each component as necessary
gasButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
carC.gas(gasAmount);
}
});
// Make the frame pack all it's components by respecting the sizes if possible.
this.pack();
// Get the computer screen resolution
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Center the frame
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
// Make the frame visible
this.setVisible(true);
// Make sure the frame exits when "x" is pressed
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}