-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
188 lines (137 loc) · 5.47 KB
/
GamePanel.java
File metadata and controls
188 lines (137 loc) · 5.47 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.util.Arrays;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements ActionListener {
static final int SCREEN_WIDTH = 1600;
static final int SCREEN_HEIGHT = 1000;
final int FPS = 30;
final int DELAY = 1000 / FPS;
Timer timer;
boolean restart;
final int BOIDS_NUMBER = 500;
Boid[] flock = new Boid[BOIDS_NUMBER];
// slider to change values
public JFrame sliderWindow;
public SliderPanel sliderPanel;
public static void main(String[] args) {
System.out.println("res: " + SCREEN_HEIGHT + "(height) x " + SCREEN_WIDTH + "(width)");
}
GamePanel() {
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT)); // set window size
this.setBackground(Color.black);
this.setDoubleBuffered(true); // all drawing from this component will be done in an offscreen painting buffer -> improves performance
this.setFocusable(true); // to use keyAdapter
this.requestFocusInWindow();
this.addKeyListener(new MyKeyAdapter());
// start slider panel
this.sliderWindow = new JFrame();
sliderWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sliderWindow.setResizable(false);
sliderWindow.setTitle("settings");
this.sliderPanel = new SliderPanel();
sliderWindow.add(this.sliderPanel);
sliderWindow.pack(); // resize sliderWindow to fit preferred size (specified in gamepanel)
sliderWindow.setLocationRelativeTo(null); // specify location of the sliderWindow // unll -> display at center of screen
sliderWindow.setVisible(true);
System.out.println("ready.");
}
public void start() {
restart = false;
sliderPanel.reset();
System.out.println("game loop running, fps: " + FPS);
if (timer == null) {
timer = new Timer(DELAY, this);
timer.setRepeats(true);
timer.start();
}
// populate boid array
for (int i = 0; i < flock.length; i++) {
flock[i] = new Boid(SCREEN_WIDTH, SCREEN_HEIGHT);
}
}
public class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
// System.out.println(e.getKeyCode());
switch (e.getKeyCode()) {
case 32:
// if space pressed
restart = true;
System.out.print("restarted: ");
break;
default:
break;
}
}
}
// called when the timer ends
public void actionPerformed(ActionEvent event) {
// update the screen
//System.out.println("starting new cycle");
// System.out.print(this.sliderPanel.getAlignmentMult());
// System.out.print(" - ");
// System.out.print(this.sliderPanel.getCohesionMult());
// System.out.print(" - ");
// System.out.print(this.sliderPanel.getSeparationMult());
// System.out.println();
if (restart) start();
// move all boids of the flock
for (Boid b: flock){
b.align(flock, this.sliderPanel.getAlignmentMult(), 1);
b.cohesion(flock, this.sliderPanel.getCohesionMult(), 1);
b.separation(flock, this.sliderPanel.getSeparationMult(), 1);
b.update(SCREEN_WIDTH, SCREEN_HEIGHT);
}
repaint(); // to call paintComponent
}
// some constants to create the boids triangles
int L = 10;
int L2 = 5;
int L3 = 7;
// called by repaint()
public void paintComponent(Graphics g) {
// builtin method
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g; // 2d gives more access on geometry, coords, ...
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (Boid b: flock) {
g2.setColor(new Color(b.colorR, b.colorG, b.colorB));
//g2.fillOval((int)b.position.x() - 1, (int)b.position.y() - 1, 2, 2); // actual point
// find inclination angle
// by doing angle * 180 / PI it changes too rapidly
// solution + 90 fixes orientation issues, that i have no clue why there were
// there in the first place
double angle = Math.atan2(b.velocity.y(), b.velocity.x()) + 90;
// setup transform
AffineTransform oldTransform = g2.getTransform();
AffineTransform transform = new AffineTransform();
transform.rotate(angle, b.position.x(), b.position.y());
g2.setTransform(transform);
// draw triangles
// can use drawPolygon or fillPolygon
g2.fillPolygon(new int[]{
(int)b.position.x(),
(int)b.position.x() + L2,
(int)b.position.x() - L2
},
new int[]{
(int)b.position.y() - L,
(int)b.position.y() + L3,
(int)b.position.y() + L3
}, 3);
g2.setTransform(oldTransform);
}
g2.dispose();
}
}