Repository navigation
Expand file tree
/
Copy pathTable.java
More file actions
221 lines (170 loc) · 4.91 KB
/
Copy pathTable.java
File metadata and controls
221 lines (170 loc) · 4.91 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
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
package ballsTable;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Table extends JFrame {
private static int quantity = 0;
private int id;
private Glass glass;
private final int INTERVAL = 100;
private final int STEPS = 10;
private WindowEventListener window = new WindowEventListener();
private KeyEventListener keyboard = new KeyEventListener();
private TimeEventListener time = new TimeEventListener();
public Table(int width, int height) {
this.setTitle("Ball Table");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.glass = new Glass();
this.glass.setSize(width, height);
this.add(this.glass);
this.setSize(width, height);
this.center();
this.setVisible(true);
this.startTimer(this.INTERVAL);
this.addWindowListener(window);
this.addKeyListener(keyboard);
this.glass.addMouseListener();
Table.quantity++;
this.id = Table.quantity;
}
public String toString() {
return "Table " + this.id;
}
private void startTimer(int interval) {
Timer timer = new Timer(interval, (ActionListener) this.time);
timer.start();
}
private void center() {
Dimension winSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) (winSize.getWidth() - this.getWidth())/2;
int y = (int) (winSize.getHeight() - this.getHeight())/2;
this.setLocation(x, y);
}
public void addBall() {
Ball ball = new Ball(this.glass);
this.glass.addShape(ball);
ball.start(this.STEPS);
}
class Glass extends JPanel {
private ArrayList<Shape> shape = new ArrayList<Shape>();
private ArrayList<Color> color = new ArrayList<Color>();
private MouseEventListener mouse = new MouseEventListener();
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(int i=0; i<this.color.size(); i++) {
g2d.setPaint(this.color.get(i));
g2d.fill(this.shape.get(i));
}
}
public String toString() {
return "Glass " + id;
}
public void addMouseListener() {
super.addMouseListener(this.mouse);
}
public int getShapeQuantity() {
return this.shape.size();
}
public Shape getShape(int index) {
return this.shape.get(index);
}
public Color getShapeColor(int index) {
return this.color.get(index);
}
public void addShape(Shape shape) {
this.color.add(getRandomColor());
this.shape.add(shape);
}
private Color getRandomColor() {
int R = getIntRandom(0, 255);
int G = getIntRandom(0, 255);
int B = getIntRandom(0, 255);
return new Color(R, G, B);
}
// Return random numbers between the MIN and MAX values specified
private int getIntRandom(int max, int min) {
return (int) Math.floor((Math.random() * (max - min + 1) + min));
}
}
class TimeEventListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
for(Shape ball: glass.shape) {
((Ball) ball).move();
}
glass.repaint();
}
}
private class WindowEventListener extends WindowAdapter {
@Override
public void windowActivated(WindowEvent e) {
for(int i=0; i<glass.getShapeQuantity(); i++) {
((Ball) glass.getShape(i)).restart();
}
}
@Override
public void windowDeactivated(WindowEvent e) {
for(int i=0; i<glass.getShapeQuantity(); i++) {
((Ball) glass.getShape(i)).stop();
}
}
@Override
public void windowDeiconified(WindowEvent e) {
for(int i=0; i<glass.getShapeQuantity(); i++) {
((Ball) glass.getShape(i)).restart();
}
}
@Override
public void windowIconified(WindowEvent e) {
for(int i=0; i<glass.getShapeQuantity(); i++) {
((Ball) glass.getShape(i)).stop();
}
}
}
class KeyEventListener extends KeyAdapter {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar() == '+') {
for(int i=0; i<glass.getShapeQuantity(); i++) {
((Ball) glass.getShape(i)).grow();
}
}else if (e.getKeyChar() == '-') {
for(int i=0; i<glass.getShapeQuantity(); i++) {
((Ball) glass.getShape(i)).reduce();
}
}
}
}
class MouseEventListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
for(int i=glass.getShapeQuantity()-1; i>=0; i--) {
Ball ball = (Ball) glass.getShape(i);
Color color = (Color) glass.getShapeColor(i);
if(ball.isIn(e.getX(), e.getY())) {
ball.destroy();
glass.shape.remove(ball);
glass.color.remove(color);
break;
}
}
}
}
}