This repository has been archived by the owner on Feb 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotatedIcon.java
262 lines (241 loc) · 7.43 KB
/
RotatedIcon.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
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
252
253
254
255
256
257
258
259
260
261
262
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import javax.swing.Icon;
/**
* The RotatedIcon allows you to change the orientation of an Icon by
* rotating the Icon before it is painted. This class supports the following
* orientations:
*
* <ul>
* <li>DOWN - rotated 90 degrees
* <li>UP (default) - rotated -90 degrees
* <li>UPSIDE_DOWN - rotated 180 degrees
* <li>ABOUT_CENTER - the icon is rotated by the specified degrees about its center.
* </ul>
*
* @author Rob Camick
* Accessed at: https://tips4java.wordpress.com/2009/04/06/rotated-icon/
* This is NOT my work - the author is Rob Camick. It is only being used to reduce time spent on
* the clockFace
*
*/
public class RotatedIcon implements Icon
{
public enum Rotate
{
DOWN,
UP,
UPSIDE_DOWN,
ABOUT_CENTER;
}
private Icon icon;
private Rotate rotate;
private double degrees;
private boolean circularIcon;
/**
* Convenience constructor to create a RotatedIcon that is rotated DOWN.
*
* @param icon the Icon to rotate
*/
public RotatedIcon(Icon icon)
{
this(icon, Rotate.UP);
}
/**
* Create a RotatedIcon
*
* @param icon the Icon to rotate
* @param rotate the direction of rotation
*/
public RotatedIcon(Icon icon, Rotate rotate)
{
this.icon = icon;
this.rotate = rotate;
}
/**
* Create a RotatedIcon. The icon will rotate about its center. This
* constructor will automatically set the Rotate enum to ABOUT_CENTER.
*
* @param icon the Icon to rotate
* @param degrees the degrees of rotation
*/
public RotatedIcon(Icon icon, double degrees)
{
this(icon, degrees, false);
}
/**
* Create a RotatedIcon. The icon will rotate about its center. This
* constructor will automatically set the Rotate enum to ABOUT_CENTER.
*
* @param icon the Icon to rotate
* @param degrees the degrees of rotation
* @param circularIcon treat the icon as circular so its size doesn't change
*/
public RotatedIcon(Icon icon, double degrees, boolean circularIcon)
{
this(icon, Rotate.ABOUT_CENTER);
setDegrees( degrees );
setCircularIcon( circularIcon );
}
/**
* Gets the Icon to be rotated
*
* @return the Icon to be rotated
*/
public Icon getIcon()
{
return icon;
}
/**
* Gets the Rotate enum which indicates the direction of rotation
*
* @return the Rotate enum
*/
public Rotate getRotate()
{
return rotate;
}
/**
* Gets the degrees of rotation. Only used for Rotate.ABOUT_CENTER.
*
* @return the degrees of rotation
*/
public double getDegrees()
{
return degrees;
}
/**
* Set the degrees of rotation. Only used for Rotate.ABOUT_CENTER.
* This method only sets the degress of rotation, it will not cause
* the Icon to be repainted. You must invoke repaint() on any
* component using this icon for it to be repainted.
*
* @param degrees the degrees of rotation
*/
public void setDegrees(double degrees)
{
this.degrees = degrees;
}
/**
* Is the image circular or rectangular? Only used for Rotate.ABOUT_CENTER.
* When true, the icon width/height will not change as the Icon is rotated.
*
* @return true for a circular Icon, false otherwise
*/
public boolean isCircularIcon()
{
return circularIcon;
}
/**
* Set the Icon as circular or rectangular. Only used for Rotate.ABOUT_CENTER.
* When true, the icon width/height will not change as the Icon is rotated.
*
* @param true for a circular Icon, false otherwise
*/
public void setCircularIcon(boolean circularIcon)
{
this.circularIcon = circularIcon;
}
//
// Implement the Icon Interface
//
/**
* Gets the width of this icon.
*
* @return the width of the icon in pixels.
*/
@Override
public int getIconWidth()
{
if (rotate == Rotate.ABOUT_CENTER)
{
if (circularIcon)
return icon.getIconWidth();
else
{
double radians = Math.toRadians( degrees );
double sin = Math.abs( Math.sin( radians ) );
double cos = Math.abs( Math.cos( radians ) );
int width = (int)Math.floor(icon.getIconWidth() * cos + icon.getIconHeight() * sin);
return width;
}
}
else if (rotate == Rotate.UPSIDE_DOWN)
return icon.getIconWidth();
else
return icon.getIconHeight();
}
/**
* Gets the height of this icon.
*
* @return the height of the icon in pixels.
*/
@Override
public int getIconHeight()
{
if (rotate == Rotate.ABOUT_CENTER)
{
if (circularIcon)
return icon.getIconHeight();
else
{
double radians = Math.toRadians( degrees );
double sin = Math.abs( Math.sin( radians ) );
double cos = Math.abs( Math.cos( radians ) );
int height = (int)Math.floor(icon.getIconHeight() * cos + icon.getIconWidth() * sin);
return height;
}
}
else if (rotate == Rotate.UPSIDE_DOWN)
return icon.getIconHeight();
else
return icon.getIconWidth();
}
/**
* Paint the icons of this compound icon at the specified location
*
* @param c The component on which the icon is painted
* @param g the graphics context
* @param x the X coordinate of the icon's top-left corner
* @param y the Y coordinate of the icon's top-left corner
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
Graphics2D g2 = (Graphics2D)g.create();
int cWidth = icon.getIconWidth() / 2;
int cHeight = icon.getIconHeight() / 2;
int xAdjustment = (icon.getIconWidth() % 2) == 0 ? 0 : -1;
int yAdjustment = (icon.getIconHeight() % 2) == 0 ? 0 : -1;
if (rotate == Rotate.DOWN)
{
g2.translate(x + cHeight, y + cWidth);
g2.rotate( Math.toRadians( 90 ) );
icon.paintIcon(c, g2, -cWidth, yAdjustment - cHeight);
}
else if (rotate == Rotate.UP)
{
g2.translate(x + cHeight, y + cWidth);
g2.rotate( Math.toRadians( -90 ) );
icon.paintIcon(c, g2, xAdjustment - cWidth, -cHeight);
}
else if (rotate == Rotate.UPSIDE_DOWN)
{
g2.translate(x + cWidth, y + cHeight);
g2.rotate( Math.toRadians( 180 ) );
icon.paintIcon(c, g2, xAdjustment - cWidth, yAdjustment - cHeight);
}
else if (rotate == Rotate.ABOUT_CENTER)
{
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setClip(x, y, getIconWidth(), getIconHeight());
g2.translate((getIconWidth() - icon.getIconWidth()) / 2, (getIconHeight() - icon.getIconHeight()) / 2);
g2.rotate(Math.toRadians(degrees), x + cWidth, y + cHeight);
icon.paintIcon(c, g2, x, y);
}
g2.dispose();
}
}