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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Go
{
public JFrame window = new JFrame();
public String[][] names = {{"add", "delete"}, {"box", "oval"}};
public JButton[] bt = new JButton[names[0].length];
public JComboBox<String> comboBox = new JComboBox<String>();
public ArrayList<ArrayList<BaseEnemy>> arrEnemy = new ArrayList<ArrayList<BaseEnemy>>();
public Go()
{
makeWindow();
makeComboBox();
makeButtons();
}
private void makeWindow()
{
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(800, 600));
panel.setOpaque(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setResizable(false);
window.add(panel);
window.pack();
window.setLayout(null);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
private void makeButtons()
{
for (int i = 0; i < bt.length; i++)
{
bt[i] = new JButton();
bt[i].setBounds(20+i*100, 20, 90, 25);
bt[i].setText(names[0][i]);
bt[i].setName(i+"");
bt[i].setForeground(Color.BLACK);
bt[i].setOpaque(false);
bt[i].setContentAreaFilled(true);
bt[i].setBorderPainted(true);
bt[i].setFocusPainted(false);
bt[i].setLayout(null);
bt[i].addActionListener(new ActionListener()
{
int b = -1;
int o = -1;
public void actionPerformed(ActionEvent e)
{
if (Integer.parseInt(((JButton) e.getSource()).getName()) == 0)
{
BaseEnemy Type = new BaseEnemy(comboBox.getSelectedIndex());
window.add(Type);
window.repaint();
if (new String("box").equals(comboBox.getSelectedItem()))
{
b++;
arrEnemy.get(0).add(Type);
}
else if (new String("oval").equals(comboBox.getSelectedItem()))
{
o++;
arrEnemy.get(1).add(Type);
}
}
else if (Integer.parseInt(((JButton) e.getSource()).getName()) == 1)
{
if (new String("box").equals(comboBox.getSelectedItem())
&& b < arrEnemy.get(0).size() - 1)
{
b++;
arrEnemy.get(0).get(b).fpsTimer.stop();
arrEnemy.get(0).get(b).fpsTimer.removeActionListener(this);
window.remove(arrEnemy.get(0).get(b));
window.repaint();
}
else if (new String("oval").equals(comboBox.getSelectedItem())
&& o < arrEnemy.get(1).size() - 1)
{
o++;
arrEnemy.get(1).get(o).fpsTimer.stop();
arrEnemy.get(1).get(o).fpsTimer.removeActionListener(this);
window.remove(arrEnemy.get(1).get(o));
window.repaint();
}
}
}
});
window.add(bt[i]);
}
}
public void makeComboBox()
{
for (int i = 0; i < names[1].length; i++)
{
arrEnemy.add(new ArrayList<BaseEnemy>());
comboBox.addItem(names[1][i]);
}
comboBox.setFont(new Font("Times New Roman", Font.BOLD, 18));
comboBox.setSelectedIndex(0);
comboBox.setBounds(20, 55, 190, 30);
((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
comboBox.setFocusable(false);
window.add(comboBox);
window.revalidate();
window.repaint();
}
public class BaseEnemy extends JLabel
{
private static final long serialVersionUID = 1L;
int x = new Random().nextInt(700 - 100) + 100;
int y = new Random().nextInt(500 - 100) + 100;
int w = new Random().nextInt(50 - 20) + 20;
int h = new Random().nextInt(50 - 20) + 20;
int[] characterXY = {x,y};
Timer fpsTimer;
int type;
public BaseEnemy(int n)
{
setBounds(x, y, w, h);
type = n;
fpsTimer = new Timer(15, new ActionListener()
{
int[] arrSpeed = {3,6,9,12};
int[] speedControlXY = {1,1};
int[] speedXY = {arrSpeed[new Random().nextInt(arrSpeed.length)],
arrSpeed[new Random().nextInt(arrSpeed.length)]};
public void actionPerformed(ActionEvent e)
{
characterXY[0] += speedXY[0];
characterXY[1] += speedXY[1];
setLocation(characterXY[0], characterXY[1]);
if (characterXY[0] + getWidth() > 700)
{
characterXY[0] = 700 - getWidth();
speedControlXY[0] *= -1;
speedXY[0] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[0];
}
else if (characterXY[0] < 0)
{
characterXY[0] = 0;
speedControlXY[0] *= -1;
speedXY[0] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[0];
}
else if (characterXY[1] < 0)
{
characterXY[1] = 0;
speedControlXY[1] *= -1;
speedXY[1] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[1];
}
else if (characterXY[1] + getHeight() > 500)
{
characterXY[1] = 500 - getHeight();
speedControlXY[1] *= -1;
speedXY[1] = arrSpeed[new Random().nextInt(arrSpeed.length)] * speedControlXY[1];
}
}
});
fpsTimer.start();
}
public void paintComponent(Graphics g)
{
if (type == 0)
{
g.setColor(new Color(120,190,255));
g.fillRect(0, 0, getWidth(), getHeight());
}
else if (type == 1)
{
g.setColor(new Color(20,190,155));
g.fillOval(0, 0, getWidth(), getHeight());
}
repaint();
}
}
public static void main(String[] args)
{
new Go();
}
} |