-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
162 lines (121 loc) · 4.59 KB
/
Main.java
File metadata and controls
162 lines (121 loc) · 4.59 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
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.event.*;
class Main{
private JFrame frame;
private Canvas canvas;
private JPanel lowerPanel;
private JPanel appBar;
private JButton addBird;
private JLabel appTitle;
private ArrayList<DynamicBird> Birds;
private ImageIcon birdAdd;
private JSlider speedChange;
private JSlider cohesionK;
private JSlider separationK;
private JSlider alignmentK;
Main(){
/*JFrame initialization*/
this.frame = new JFrame();
this.frame.setTitle("Flocking Simulator");
this.frame.setSize(800,600);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setVisible(true);
/*Canvas initialization*/
this.canvas = new Canvas();
/*JPanels initialization*/
this.lowerPanel = new JPanel();
this.appBar = new JPanel();
/*Birds initialization*/
this.Birds = new ArrayList<DynamicBird>();
Birds.add(new DynamicBird(canvas));
Birds.add(new DynamicBird(canvas));
Birds.add(new DynamicBird(canvas));
/*ImageIcon initialization*/
this.birdAdd = new ImageIcon("shape.png");
/*JButton initialization*/
addBird = new JButton(birdAdd);
addBird.setBorderPainted(false);
addBird.addActionListener( new ButtonListener());
/*JLabel initalization*/
appTitle = new JLabel(" Flocking Simulator");
appTitle.setForeground(new Color(249,249,249));
appTitle.setFont(new Font("random", Font.PLAIN, 25));
/*JSlider initialization*/
speedChange = new JSlider(0,400);
speedChange.addChangeListener( new SliderListener());
cohesionK = new JSlider(0,1);
separationK = new JSlider(0,1);
alignmentK = new JSlider(0,1);
/*JComponents settings*/
FlowLayout newFlowLayout = new FlowLayout();
GridLayout lowerGridLayout = new GridLayout(2,5);
GridLayout appBarGridLayout = new GridLayout(3,3);
lowerPanel.setLayout(lowerGridLayout);
lowerPanel.setBackground(new Color(238,238,238));
appBar.setLayout(appBarGridLayout);
appBar.setBackground(new Color(57,73,171));
this.frame.add(canvas);
lowerPanel.add(cohesionK);
lowerPanel.add(separationK);
lowerPanel.add(alignmentK);
lowerPanel.add(new Label(" Speed"));
lowerPanel.add(new Label(" "));
lowerPanel.add(new Label(" Cohesion"));
lowerPanel.add(new Label(" Alignment"));
lowerPanel.add(new Label(" Separation"));
addBird.setBackground(null);
lowerPanel.add(speedChange);
lowerPanel.add(addBird);
appBar.add(new JLabel(" "));
appBar.add(appTitle);
this.frame.add(lowerPanel, BorderLayout.PAGE_END);
this.frame.add(appBar, BorderLayout.PAGE_START);
this.gameLoop();
}
private void gameLoop(){
int deltaTime = 30;
int randomAngle = Utils.randomInt(100);
while(true){
for ( DynamicBird Dumi : Birds ){
Dumi.undraw();
}
for ( DynamicBird Dumi : Birds ){
Dumi.update(deltaTime);
}
/*
for ( DynamicBird Dumi : Birds ){
Dumi.cohesion_separation(Birds);
}
for ( DynamicBird Dumi : Birds ){
Dumi.alignment(Birds);
}
*/
for ( DynamicBird Dumi : Birds ){
Dumi.draw();
}
for ( DynamicBird Dumi : Birds ){
Dumi.wallCollision(frame);
}
Utils.pause(15);
}
}
public static void main(String[] args){
new Main();
}
class ButtonListener implements ActionListener{
public void actionPerformed (ActionEvent event) {
Birds.add(new DynamicBird(canvas));
}
}
class SliderListener implements ChangeListener{
public void stateChanged(ChangeEvent event){
int newSpeed = speedChange.getValue();
for ( DynamicBird Dumi : Birds ){
Dumi.setSpeed(newSpeed);
}
}
}
}