-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolygonButton.java
56 lines (46 loc) · 1.89 KB
/
PolygonButton.java
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
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class PolygonButton extends JButton implements ActionListener {
protected JPanel drawingPanel;
protected View view;
private MouseHandler mouseHandler;
private PolygonCommand polygonCommand;
private UndoManager undoManager;
public PolygonButton(UndoManager undoManager, View jFrame, JPanel jPanel) {
super("Polygon");
this.undoManager = undoManager;
addActionListener(this);
view = jFrame;
drawingPanel = jPanel;
mouseHandler = new MouseHandler();
}
public void actionPerformed(ActionEvent event) {
view.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
// Change cursor when button is clicked
drawingPanel.addMouseListener(mouseHandler);
// Start listening for mouseclicks on the drawing panel
}
private class MouseHandler extends MouseAdapter {
private int pointCount = 0;
public void mouseClicked(MouseEvent event) {
if (SwingUtilities.isLeftMouseButton(event)) { // Check if the click is a left click
if (++pointCount == 1) {
polygonCommand = new PolygonCommand(View.mapPoint(event.getPoint()));
undoManager.beginCommand(polygonCommand);
pointCount++;
} else if (pointCount > 0) {
polygonCommand.setPolyPoint(View.mapPoint(event.getPoint()));
polygonCommand.execute();
}
} else if (SwingUtilities.isRightMouseButton(event)) {
if (pointCount > 3) {
pointCount = 0;
drawingPanel.removeMouseListener(this);
view.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
undoManager.endCommand(polygonCommand);
}
}
}
}
}