-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCirclePanelListener.java
More file actions
82 lines (69 loc) · 2.49 KB
/
CirclePanelListener.java
File metadata and controls
82 lines (69 loc) · 2.49 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
// *** Your name:
import java.awt.Color;
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;
public class CirclePanelListener implements MouseListener, MouseMotionListener {
private CirclePanel circlePanel;
private Circle currentlyDraggedCircle = null;
private int offsetX;
private int offsetY;
private Color colorOfNewlyCreatedCircles = Color.red;
private int radiusOfNewlyCreatedCircles = 30;
public CirclePanelListener(CirclePanel panel) {
circlePanel = panel;
circlePanel.addMouseListener(this);
circlePanel.addMouseMotionListener(this);
}
public void setColorOfNewlyCreatedCircles(Color color) {
colorOfNewlyCreatedCircles = color;
}
public void setRadiusOfNewlyCreatedCircles(int radius) {
radiusOfNewlyCreatedCircles = radius;
}
@Override
public void mouseClicked(MouseEvent ev) {
//implement this method to add a circle to circlePanel
//the circle should have the color and radius specified by
//colorOfNewlyCreatedCircles and radiusOfNewlyCreatedCircles
//the center of the created circle should be at the click location
Circle newCircle = new Circle(ev.getX() - radiusOfNewlyCreatedCircles,
ev.getY() - radiusOfNewlyCreatedCircles,
radiusOfNewlyCreatedCircles, colorOfNewlyCreatedCircles);
circlePanel.addCircle(newCircle);
circlePanel.repaint();
}
@Override
public void mouseEntered(MouseEvent ev) { }
@Override
public void mouseExited(MouseEvent ev) {
}
@Override
public void mousePressed(MouseEvent ev) {
//implement this method in conjunction with the mouseDragged
//method to be able to drag circles in the circlePanel
currentlyDraggedCircle = circlePanel.containsPoint(ev.getX(), ev.getY());
if (currentlyDraggedCircle!= null) {
offsetX = ev.getX() - currentlyDraggedCircle.getX();
offsetY = ev.getY() - currentlyDraggedCircle.getY();
}
}
@Override
public void mouseReleased(MouseEvent ev) {
currentlyDraggedCircle = null;
}
@Override
public void mouseDragged(MouseEvent ev) {
//implement this method in conjunction with the mousePressed
//method to be able to drag circles in the circlePanel
if (currentlyDraggedCircle!= null) {
currentlyDraggedCircle.setX(ev.getX() - offsetX);
currentlyDraggedCircle.setY(ev.getY() - offsetY);
circlePanel.repaint();
}
}
@Override
public void mouseMoved(MouseEvent ev) { }
}