-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDots.java
More file actions
251 lines (219 loc) · 6.07 KB
/
Dots.java
File metadata and controls
251 lines (219 loc) · 6.07 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.awt.*;
import java.util.List;
import java.util.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
//Dots.java Amy Liu
/*
* Dots is a program that allows you to place dots on the screen
* and counts the number of dots on the screen.
* Features:
* - Each dot is a different random color. Changes color each time you use
* one of the features unless setting to a solid color.
* - Change size of dots
* - Scroll up using mouse to increase size
* - Scroll down using mouse to decrease size
* - Click and drag mouse to place a series of dots along mouse path
* - Dot follows mouse path
* - Dot is green when using random color
* - Dot is the same color as the other dots when using specific color
* - Clear screen
* - Press DELETE on keyboard
* - Erase last dot placed
* - Press SPACE on keyboard
* - Enable random color
* - Press CONTROL on keyboard
* - Change color of dots to red
* - Press R on keyboard
* - Change color of dots to green
* - Press G on keyboard
* - Change color of dots to blue
* - Press B on keyboard
* - Change color of dots to white
* - Press W on keyboard
* - Change color of dots to yellow
* - Press Y on keyboard
* - Change color of dots to magenta
* - Press M on keyboard
* - Change color of dots to orange
* - Press O on keyboard
* - Change color of dots to pink
* - Press P on keyboard
*/
@SuppressWarnings("serial")
public class Dots extends JPanel{
private final int WIDTH = 700, HEIGHT = 500;
private int radius;
private List<Point> pointList;
private int count;
private Point mousePoint = new Point();
private Color color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
private boolean randomColor = true;
public Dots() {
pointList = new ArrayList<Point>();
count = 0;
radius = 6;
addMouseListener(new DotsListener());
addMouseWheelListener(new DotsListener());
addMouseMotionListener(new DotsListener());
addKeyListener(new DotsListener());
setBackground(Color.black);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
// Necessary to get KeyboardListener to work
public void addNotify() {
super.addNotify();
requestFocus();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
for (Point point : pointList)
{
if(randomColor)
{
g.setColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
}
g.fillOval(point.x - radius, point.y - radius, radius * 2, radius * 2);
}
if(randomColor)
{
g.setColor(Color.green);
}
g.fillOval(mousePoint.x - radius, mousePoint.y - radius, radius * 2, radius * 2);
g.setColor(Color.green);
g.drawString("Count: " + count, 5, 15);
}
private class DotsListener implements MouseListener, MouseWheelListener, MouseMotionListener, KeyListener {
//Mouse
@Override
public void mousePressed(MouseEvent e) {
pointList.add(e.getPoint());
count++;
repaint();
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
//MouseWheel
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
{
if(radius < 0)
radius = 0;
else
radius -= e.getWheelRotation();
repaint();
}
}
//MouseMotion
@Override
public void mouseDragged(MouseEvent e) {
pointList.add(e.getPoint());
count++;
mouseMoved(e);
repaint();
}
public void mouseMoved(MouseEvent e) {
mousePoint = e.getPoint();
repaint();
}
//Key
@Override
public void keyPressed(KeyEvent e) {
//Clear all dots
if(e.getKeyCode() == KeyEvent.VK_DELETE)
{
pointList.clear();
count = 0;
repaint();
}
//Erase last dot
else if(pointList.size() != 0 && e.getKeyCode() == KeyEvent.VK_SPACE)
{
pointList.remove(pointList.size() - 1);
count--;
repaint();
}
//Enable random color
else if(e.getKeyCode() == KeyEvent.VK_CONTROL)
{
randomColor = true;
repaint();
}
//Change color to red
else if (e.getKeyCode() == KeyEvent.VK_R)
{
color = Color.RED;
randomColor = false;
repaint();
}
//Change color to green
else if (e.getKeyCode() == KeyEvent.VK_G)
{
color = Color.GREEN;
randomColor = false;
repaint();
}
//Change color to blue
else if (e.getKeyCode() == KeyEvent.VK_B)
{
color = Color.BLUE;
randomColor = false;
repaint();
}
//Change color to white
else if (e.getKeyCode() == KeyEvent.VK_W)
{
color = Color.WHITE;
randomColor = false;
repaint();
}
//Change color to yellow
else if (e.getKeyCode() == KeyEvent.VK_Y)
{
color = Color.YELLOW;
randomColor = false;
repaint();
}
//Change color to magenta
else if (e.getKeyCode() == KeyEvent.VK_M)
{
color = Color.MAGENTA;
randomColor = false;
repaint();
}
//Change color to orange
else if (e.getKeyCode() == KeyEvent.VK_O)
{
color = Color.ORANGE;
randomColor = false;
repaint();
}
//Change color to pink
else if (e.getKeyCode() == KeyEvent.VK_P)
{
color = Color.PINK;
randomColor = false;
repaint();
}
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}
public static void main(String[] args) {
JFrame dotsFrame = new JFrame("Dots");
dotsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dotsFrame.getContentPane().add(new Dots());
dotsFrame.pack();
dotsFrame.setVisible(true);
}
}