-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwingDisplay.java
149 lines (126 loc) · 3.55 KB
/
SwingDisplay.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import iiitb.ess201a7.a7base.Car;
import iiitb.ess201a7.a7base.Location;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class SwingDisplay extends Display {
private JFrame frame;
private JPanel canvas = null;
private Color[] colors = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW,
Color.MAGENTA, Color.CYAN };
private ArrayList<Car> cars;
private ArrayList<Line> lines;
private int startX, startY, endX, endY;
private int selections = 0;
public SwingDisplay() {
cars = new ArrayList<Car>();
lines = new ArrayList<Line>();
}
@Override
public void init() {
System.out.println("Starting new simulation");
// start the GUI in the EDT thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setupGUI();
}
});
}
@Override
public void draw(Car car) {
// TODO Auto-generated method stub
cars.add(car);
}
@Override
public void drawLine(Location a, Location b) {
// TODO Auto-generated method stub
lines.add(new Line(a,b));
}
@Override
public void update() {
if(canvas != null) {
canvas.repaint();
}
}
@SuppressWarnings("serial")
private void setupGUI() {
frame = new JFrame("Map View of Fleets");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// set up a canvas for drawing the position of the cars
// implement how cars should be rendered by overriding paintComponent
canvas = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int i = 0;
for(Car car: cars) {
g.setColor(colors[i%6]);
i++;
Location loc = car.getLocation();
g.drawOval(loc.getX(), loc.getY(), 10, 10);
g.drawString("" + car.getId() + "-"
+ car.getStatus(), loc.getX(), loc.getY() );
}
for(Line line: lines) {
g.setColor(colors[i%6]);
i++;
g.drawLine(line.getStart().getX(), line.getStart().getY()
, line.getEnd().getX(), line.getEnd().getY());
}
cars.clear();
lines.clear();
}
};
canvas.setPreferredSize(new Dimension(1000, 1000));
frame.add(canvas, BorderLayout.LINE_START);
JButton b1 = new JButton("New Trip");
frame.add(b1, BorderLayout.PAGE_START);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// request trip
if (selections == 2) {
requestTrip(new Location(startX, startY), new Location(endX, endY));
selections = 0;
}
}
});
JButton b2 = new JButton("Exit");
frame.add(b2, BorderLayout.PAGE_END);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(selections == 0) {
startX = e.getX();
startY = e.getY();
selections++;
} else if (selections == 1) {
endX = e.getX();
endY = e.getY();
selections++;
}
}
});
// Display the window.
frame.pack();
frame.setVisible(true); // starts EDT if not already started
// EDT continues until explicitly terminated with exit (or exception)
}
}
class Line {
private Location startp, endp;
Line(Location s, Location e) {
startp = s;
endp = e;
}
Location getStart() { return startp; }
Location getEnd() { return endp; }
}