-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathCarController.java
65 lines (54 loc) · 2.1 KB
/
CarController.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
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
/*
* This class represents the Controller part in the MVC pattern.
* It's responsibilities is to listen to the View and responds in a appropriate manner by
* modifying the model state and the updating the view.
*/
public class CarController {
// member fields:
// The delay (ms) corresponds to 20 updates a sec (hz)
private final int delay = 50;
// The timer is started with a listener (see below) that executes the statements
// each step between delays.
private Timer timer = new Timer(delay, new TimerListener());
// The frame that represents this instance View of the MVC pattern
CarView frame;
// A list of cars, modify if needed
// ArrayList<ACar> cars = new ArrayList<>();
//methods:
public static void main(String[] args) {
// Instance of this class
CarController cc = new CarController();
// cc.cars.add(new Volvo240());
// Start a new view and send a reference of self
cc.frame = new CarView("CarSim 1.0", cc);
// Start the timer
cc.timer.start();
}
/* Each step the TimerListener moves all the cars in the list and tells the
* view to update its images. Change this method to your needs.
* */
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
/* for (ACar car : cars) {
car.move();
int x = (int) Math.round(car.getPosition().getX());
int y = (int) Math.round(car.getPosition().getY());
frame.drawPanel.moveit(x, y);
// repaint() calls the paintComponent method of the panel
frame.drawPanel.repaint();
}*/
}
}
// Calls the gas method for each car once
void gas(int amount) {
double gas = ((double) amount) / 100;
/* for (ACar car : cars
) {
car.gas(gas);
}*/
}
}