-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrowButton.java
156 lines (129 loc) · 4.76 KB
/
ArrowButton.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
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
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
/**
* Produces an arrow shaped button.
*
* @author Jonathan Beaumont
*/
public class ArrowButton extends JButton {
private static final long serialVersionUID = 5390515140812418112L;
private Polygon shape;
private static final HashMap<Character, Integer[][]> TRIANGLE_COORDINATES = new HashMap<Character, Integer[][]>() {
private static final long serialVersionUID = -4471808136694367835L;
{
put ('N', new Integer[][] {{20, 20, 0, 25, 50, 30, 30}, {50, 25, 25, 0, 25, 25, 50}});
put ('W', new Integer[][] {{25, 0, 25, 25, 50, 50, 25}, {0, 25, 50, 30, 30, 20, 20}});
put ('E', new Integer[][] {{25, 50, 25, 25, 0, 0, 25}, {0, 25, 50, 30, 30, 20, 20}});
put ('S', new Integer[][] {{20, 20, 0, 25, 50, 30, 30}, {0, 25, 25, 50, 25, 25, 0}});
}};
private static final Color NORMAL_COLOR = Color.BLACK;
private static final Color HOVER_COLOR = Color.DARK_GRAY;
private static final Color PRESS_COLOR = Color.GRAY;
/**
* Constructor. Sets up the direction of the arrow and set the
* formatting of the button.
*
* @param direction if the direction for the arrow to point in.
* @throws InvalidDirectionException if the direction is not N/E/S/W
*/
public ArrowButton(char direction) throws InvalidDirectionException {
if (isValidDirection(direction)) {
// Gets the x and y points for the arrow
int[] xPoints = convertIntegerArrayToPrimitive(TRIANGLE_COORDINATES.get(direction)[0]);
int[] yPoints = convertIntegerArrayToPrimitive(TRIANGLE_COORDINATES.get(direction)[1]);
// Sets the shape of the button
this.shape = new Polygon(xPoints, yPoints, 7);
// Sets up and forces the size
this.setSize(new Dimension(50, 50));
this.setMinimumSize(this.getSize());
this.setMaximumSize(this.getSize());
this.setPreferredSize(this.getSize());
// Sets up the background and stops
this.setForeground(NORMAL_COLOR);
this.setBorderPainted(false);
this.setOpaque(false);
this.setContentAreaFilled(false);
} else {
// If the direction is not valid
throw new InvalidDirectionException(direction);
}
addMouseListener(new ArrowButtonMouseListener());
}
/**
* @param direction is a char direction
* @return if the direction is one of N/E/S/W
*/
private boolean isValidDirection(char direction) {
return TRIANGLE_COORDINATES.containsKey(direction);
}
/**
* Paints the arrow shaped polygon when the background is being
* changed.
*
* @param g graphics object to edit
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gCopy = (Graphics2D) g.create();
gCopy.fillPolygon(this.shape);
g.dispose();
}
@Override
public boolean contains(int x, int y) {
return this.shape.contains(x, y);
}
@Override
protected void paintBorder(Graphics g) {
}
private int[] convertIntegerArrayToPrimitive(Integer[] objectArray) {
int[] primitiveArray = new int[objectArray.length];
for (int i = 0; i < objectArray.length; i++)
primitiveArray[i] = objectArray[i];
return primitiveArray;
}
private void enableNormalColor() {
setForeground(NORMAL_COLOR);
}
private void enableHoverColor() {
if (isEnabled())
setForeground(HOVER_COLOR);
else
enableNormalColor();
}
private void enablePressColor() {
if (isEnabled())
setForeground(PRESS_COLOR);
else
enableNormalColor();
}
class ArrowButtonMouseListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
enablePressColor();
}
@Override
public void mouseReleased(MouseEvent e) {
enableHoverColor();
}
@Override
public void mouseEntered(MouseEvent e) {
enableHoverColor();
}
@Override
public void mouseExited(MouseEvent e) {
enableNormalColor();
}
}
}
class InvalidDirectionException extends Exception {
public InvalidDirectionException(char direction) {
super("Unrecognised direction : " + direction);
}
}