-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
51 lines (42 loc) · 1.19 KB
/
App.java
File metadata and controls
51 lines (42 loc) · 1.19 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class App extends JPanel implements ActionListener, Constants {
public App() {
this.setPreferredSize(new Dimension(Constants.WIDTH * Constants.SIZE, Constants.HEIGHT * Constants.SIZE));
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.addMouseListener(new MyMouseListener(this));
start();
}
Timer timer;
static List<Sand> grains;
public void start() {
timer = new Timer(DELAY, this);
timer.start();
grains = new ArrayList<>();
}
public void moveAll() {
for (Sand grain : grains) {
grain.move();
}
}
public static List<Sand> getGrains() {return grains;}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
g.setColor(Color.blue);
for (Sand grain : grains) {
grain.draw(g, grain.getX(), grain.getY());
}
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
moveAll();
repaint();
}
}