-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOutlineIcon.java
More file actions
227 lines (193 loc) · 4.53 KB
/
OutlineIcon.java
File metadata and controls
227 lines (193 loc) · 4.53 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
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import javax.swing.Icon;
/**
* An Icon that will paint a filled Shape with a specified Color.
*/
public class OutlineIcon implements Icon
{
private Shape shape;
private Color color;
private int thickness;
private boolean antiAliasing = true;
private BasicStroke stroke;
private Rectangle strokeBounds;
/**
* Create a OutlineIcon with a BLACK outline 1 pixel thick
*
* @param shape the Shape
*/
public OutlineIcon(Shape shape)
{
this(shape, Color.BLACK, 1);
}
/**
* Create a OutlineIcon with an outline using the
* specified Color and a thickness of 1 pixel
*
* @param shape the Shape
* @param color the fill Color of the Shape
*/
public OutlineIcon(Shape shape, Color color)
{
this(shape, color, 1);
}
/**
* Create a OutlineIcon with a BLACK outline using the
* specified thickness.
*
* @param shape the Shape
* @param thickness the thickness of the Shape outline
*/
public OutlineIcon(Shape shape, int thickness)
{
this(shape, Color.BLACK, thickness);
}
/**
* Create a OutlineIcon with an outline of the specified
* Color and thicness
*
* @param shape the Shape
* @param color the Color of the Shape outline
* @param thickness the thickness of the Shape outline
*/
public OutlineIcon(Shape shape, Color color, int thickness)
{
setShape( shape );
setColor( color );
setThickness( thickness );
}
/**
* Get the Color of the Shape
*
* @returns the Color of the Shape
*/
public Color getColor()
{
return color;
}
/**
* Set the Color of the Shape
*
* @param color the Color of the Shape
*/
public void setColor(Color color)
{
this.color = color;
}
/**
* Get the Shape
*
* @returns the Shape
*/
public Shape getShape()
{
return shape;
}
/**
* Set the Shape
*
* @param shape the Shape to be painted
*/
public void setShape(Shape shape)
{
this.shape = shape;
resetStroke();
}
/**
* Get the Shape outline thickness
*
* @returns the Shape outline thickness
*/
public int getThickness()
{
return thickness;
}
/**
* Set the Shape outline thickness
*
* @param thickness the Shape outline thickness
*/
public void setThickness(int thickness)
{
this.thickness = thickness;
resetStroke();
}
/**
* Use AntiAliasing when painting the shape
*
* @returns true for AntiAliasing false otherwise
*/
public boolean isAntiAliasing()
{
return antiAliasing;
}
/**
* Set AntiAliasing property for painting the Shape
*
* @param antiAliasing true for AntiAliasing, false otherwise
*/
public void setAntiAliasing(boolean antiAliasing)
{
this.antiAliasing = antiAliasing;
}
private void resetStroke()
{
stroke = new BasicStroke( thickness );
strokeBounds = stroke.createStrokedShape(shape).getBounds();
}
/**
* {@inheritDoc}
*/
@Override
public int getIconWidth()
{
int width = strokeBounds.width + getAdjustment();
return width;
}
/**
* {@inheritDoc}
*/
@Override
public int getIconHeight()
{
int height = strokeBounds.height + getAdjustment();
return height;
}
private int getAdjustment()
{
// For odd thicknesses
if (thickness %2 == 1)
return -1;
// For even thicknesses we also need to check for anti aliasing
return (isAntiAliasing()) ? 1 : 0;
}
/**
* {@inheritDoc}
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
// Use Graphics2D so we can do antialiasing
Graphics2D g2d = (Graphics2D)g.create();
if (antiAliasing)
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Paint the Shape outline at the top/left position of the icon.
// Need to do an adjustment when using an odd Stroke thickness
int shift = (thickness % 2 == 0) ? 0 : -1;
// Handle Icon position within the component and Shape translation
// (ie. X/Y positions in bounding rectangle are ignored)
g2d.translate(x - strokeBounds.x + shift, y - strokeBounds.y + shift);
// Fill the Shape with the specified Color
g2d.setStroke( stroke );
g2d.setColor(color);
g2d.draw( shape );
g2d.dispose();
}
}