-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSnowmanMaker.java
More file actions
116 lines (89 loc) · 2.4 KB
/
Copy pathSnowmanMaker.java
File metadata and controls
116 lines (89 loc) · 2.4 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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
Exam 2 recursive graphics question.
@author Jim Teresco
@version Spring 2020
*/
public class SnowmanMaker extends MouseAdapter implements Runnable {
// list of snowmen currently on the screen
private java.util.List<Snowman> list;
private JPanel panel;
// Snowman being defined now
private Point center;
private Point dragPoint;
/**
The run method to set up the graphical user interface
*/
@Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SnowmanMaker");
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);
// in-progess, if there is one
if (dragPoint != null) {
int radius = (int)center.distance(dragPoint);
g.drawOval(center.x - radius,
center.y - radius,
radius*2, radius*2);
}
// existing snowmen
for (Snowman s : list) {
s.paint(g);
}
}
};
frame.add(panel);
panel.addMouseListener(this);
panel.addMouseMotionListener(this);
list = new ArrayList<Snowman>();
frame.pack();
frame.setVisible(true);
}
/**
Mouse press event handler to create a new Snowman centered at
the press point.
@param e mouse event info
*/
@Override
public void mousePressed(MouseEvent e) {
center = e.getPoint();
dragPoint = null;
panel.repaint();
}
/**
Mouse press event handler to show the "rubber-banding" circle to
define the snowman's base.
@param e mouse event info
*/
@Override
public void mouseDragged(MouseEvent e) {
dragPoint = e.getPoint();
panel.repaint();
}
/**
Mouse release event handler to stop the growth of the most recently
created bubble.
@param e mouse event info
*/
@Override
public void mouseReleased(MouseEvent e) {
Snowman newSnowman = new Snowman(center, e.getPoint());
list.add(newSnowman);
center = null;
dragPoint = null;
panel.repaint();
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new SnowmanMaker());
}
}