-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingDisplay.java
More file actions
163 lines (130 loc) · 4.04 KB
/
Copy pathSwingDisplay.java
File metadata and controls
163 lines (130 loc) · 4.04 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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.*;
import flockbase.Bird;
import flockbase.Position;
class SwingDisplay implements FlockDisplay {
private Color[] colors = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW,
Color.MAGENTA, Color.BLACK };
private JFrame dframe, gframe;
private JPanel canvas = null;
SwingDisplay() {
birds = new ArrayList<Bird>();
}
@Override
public void init() {
// start the GUI in the EDT thread
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setupGUI();
}
});
}
@Override
public void draw(Bird Bird) {
// Just maintain the list of active birds
// will get drawn when canvas gets updated/repainted
birds.add(Bird);
}
@Override
public void update() {
if(canvas != null) {
// forces paintComponent to get invoked
canvas.repaint();
}
}
@SuppressWarnings("serial")
private void setupGUI() {
dframe = new JFrame("View of Flocks");
dframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dframe.setLayout(new BorderLayout());
// set up a canvas for drawing the position of the Birds
// implement how Birds should be rendered by overriding paintComponent
canvas = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int i = 0;
for(Bird bird: birds) {
g.setColor(colors[i%6]);
i++;
Position loc = bird.getPos();
//System.out.println(loc);
// next few lines can be replaced with any other interesting way of drawing birds
g.drawOval(loc.getX(), loc.getY(), 10, 10);
g.drawString(bird.getName(),
loc.getX() , loc.getY() );
}
// done with drawing bird - not needed anymore
birds.clear();
}
};
canvas.setPreferredSize(new Dimension(1000, 1000));
// clicking anywhwere on canvas should be interpreted as setting a new target for flock
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Clicked " + e.getX() + ", " + e.getY());
app.setTarget(e.getX(), e.getY());
}
});
dframe.add(canvas, BorderLayout.CENTER);
// create button controls for various app activities
gframe = new JFrame("Controls");
gframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gframe.setLayout(new BorderLayout());
// "Start" button to star the simulation
JButton b0 = new JButton("Start");
gframe.add(b0, BorderLayout.PAGE_START);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
app.start();
}
}
);
// "Split Flock" to split the current flock into 2
JButton b1 = new JButton("Split Flock");
gframe.add(b1, BorderLayout.LINE_START);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// request trip
app.splitFlock();
}
}
);
// "Merge Flock" tp merge flocks back into one
JButton b2 = new JButton("Merge Flock");
gframe.add(b2, BorderLayout.LINE_END);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// request trip
app.joinFlocks();
}
}
);
// "Exit" button to allow graceful exit
JButton b3 = new JButton("Exit");
gframe.add(b3, BorderLayout.PAGE_END);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
dframe.pack();
dframe.setVisible(true); // starts EDT if not already started
gframe.pack();
gframe.setVisible(true);
// EDT continues until explicitly terminated with exit (or exception)
}
@Override
public
void setApp(App appC) {
// TODO Auto-generated method stub
app = appC;
}
private App app; // to allow communication back to app
private ArrayList<Bird> birds; // temporarily needed for redisplay
}