-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOutlineComponent.java
More file actions
274 lines (231 loc) · 6.35 KB
/
OutlineComponent.java
File metadata and controls
274 lines (231 loc) · 6.35 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
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
263
264
265
266
267
268
269
270
271
272
273
274
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import javax.swing.JComponent;
/**
* A component that paints the outline of a Shape object. Click detection will
* be determined by the Shape itself, not the bounding Rectangle of the Shape.
*
* Shape objects can be created with an X/Y offset. These offsets will
* be ignored and the Shape outline will always be painted at (0, 0) so the
* outline is fully contained within the component.
*/
public class OutlineComponent extends JComponent
{
private Shape shape;
private int thickness = 1;
private boolean antiAliasing = true;
private BasicStroke stroke;
private Rectangle strokeBounds;
/**
* Create a OutlineComponent with a 1 pixel BLACK outline
*
* @param shape the shape to be painted
*/
public OutlineComponent(Shape shape)
{
this(shape, Color.BLACK, 1);
}
/**
* Create a OutlineComponent with a 1 pixel outline painted in the
* specified color
*
* @param shape the shape to be painted
* @param color the outline color of the shape
*/
public OutlineComponent(Shape shape, Color color)
{
this(shape, color, 1);
}
/**
* Create a OutlineComponent with a BLACK outline painted at the
* specified thickness
*
* @param shape the shape to be painted
* @param thickness the thickness of the outline
*/
public OutlineComponent(Shape shape, int thickness)
{
this(shape, Color.BLACK, thickness);
}
/**
* Create a OutlineComponent with the outline painted in the specified
* color and thickness
*
* @param shape the Shape outline to be painted
* @param color the color of the outline
* @param thicknes the thickness of the outline
*/
public OutlineComponent(Shape shape, Color color, int thickness)
{
setShape( shape );
setForeground( color );
setThickness( thickness );
setOpaque( false );
}
/**
* Get the Shape of the component
*
* @returns the the Shape of the compnent
*/
public Shape getShape()
{
return shape;
}
/**
* Set the Shape for this component
*
* @param shape the Shape of the component
*/
public void setShape(Shape shape)
{
this.shape = shape;
resetStroke();
revalidate();
repaint();
}
/**
* Get the thickness of the Shape outline when painted
*
* @return the Shape outline thickness
*/
public int getThickness()
{
return thickness;
}
/**
* Set the Shape outline thickness
*
* @param thickness the Shape outline in pixels
*/
public void setThickness(int thickness)
{
this.thickness = thickness;
resetStroke();
revalidate();
repaint();
}
/**
* 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;
revalidate();
repaint();
}
private void resetStroke()
{
stroke = new BasicStroke( thickness );
strokeBounds = stroke.createStrokedShape(shape).getBounds();
}
/**
* {@inheritDoc}
*/
@Override
public Dimension getPreferredSize()
{
// Include Border insets and Shape bounds
Insets insets = getInsets();
int adjustment = getAdjustment();
// Determine the preferred size
int width = insets.left + insets.right + strokeBounds.width + adjustment;
int height = insets.top + insets.bottom + strokeBounds.height + adjustment;
return new Dimension(width, 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 Dimension getMinimumSize()
{
return getPreferredSize();
}
/**
* {@inheritDoc}
*/
@Override
public Dimension getMaximumSize()
{
return getPreferredSize();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
// Graphics2D is required for antialiasing and painting Shapes
Graphics2D g2d = (Graphics2D)g.create();
if (isAntiAliasing())
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// The draw() method will paint the Shape outline (up/left) of the
// filled Shape. Since we want all the painting to be done at a zero
// offset we must manully translate the Shape (down/right) before it
// is painted.
int shift = getShift();
// Shape translation (ie. non-zero X/Y position in bounding rectangle)
// and Border insets.
Insets insets = getInsets();
// Do all translations at once
int translateX = insets.left - strokeBounds.x + shift;
int translateY = insets.top - strokeBounds.y + shift;
g2d.translate(translateX, translateY);
// Draw the Shape with the specified Color and thickness
g2d.setStroke( stroke );
g2d.setColor( getForeground());
g2d.draw( shape );
g2d.dispose();
}
/**
* This method will only determine if the point is in the bounds of the
* Shape. The outline of the Shape as painted by the draw() method
* is not considered part of the Shape.
*
* {@inheritDoc}
*/
@Override
public boolean contains(int x, int y)
{
Insets insets = getInsets();
int shift = getShift();
// Check to see if the Shape contains the point. Take into account
// the Shape X/Y coordinates, Border insets and Shape translation.
int translateX = x + strokeBounds.x - insets.left;
int translateY = y + strokeBounds.y - insets.top;
return shape.contains(translateX, translateY);
}
/**
* The painting of the Shape outline may be shifted depending on the
* stroke thickness. The painting is shifted to make sure it starts
* at the top/left of the component.
*/
private int getShift()
{
int shift = (thickness % 2 == 0) ? 0 : -1;
return shift;
}
}