-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpThenDownGame.java
More file actions
85 lines (62 loc) · 1.73 KB
/
Copy pathUpThenDownGame.java
File metadata and controls
85 lines (62 loc) · 1.73 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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
Exam 2 animation question.
@author Jim Teresco
@version Spring 2020
*/
public class UpThenDownGame extends MouseAdapter implements Runnable {
// list of up/down ball objects currently on the screen
private java.util.List<UpDownBall> list;
private JPanel panel;
/**
The run method to set up the graphical user interface
*/
@Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("UpThenDownGame");
frame.setPreferredSize(new Dimension(800,800));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JPanel with a paintComponent method
panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// display the Up/Down Ball objects
for (UpDownBall b : list) {
b.paint(g);
}
}
};
frame.add(panel);
panel.addMouseListener(this);
list = new ArrayList<UpDownBall>();
frame.pack();
frame.setVisible(true);
}
/**
Mouse press event handler to create a new UpDownBall centered
at the press point.
@param e mouse event info
*/
@Override
public void mousePressed(MouseEvent e) {
list.add(new UpDownBall(e.getPoint(), panel));
panel.repaint();
}
/**
Mouse release event handler that will cause the just-created ball
to stop moving up and start moving down.
@param e mouse event info
*/
@Override
public void mouseReleased(MouseEvent e) {
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new UpThenDownGame());
}
}