-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShapeIcon.java
137 lines (119 loc) · 2.57 KB
/
ShapeIcon.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
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 ShapeIcon implements Icon
{
private Shape shape;
private Color color;
private boolean antiAliasing = true;
/**
* Create a ShapeIcon
*
* @param shape the Shape
* @param color the fill Color of the Shape
*/
public ShapeIcon(Shape shape, Color color)
{
this.shape = shape;
this.color = color;
}
/**
* 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;
}
/**
* 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;
}
/**
* {@inheritDoc}
*/
@Override
public int getIconWidth()
{
Rectangle bounds = shape.getBounds();
return bounds.width;
}
/**
* {@inheritDoc}
*/
@Override
public int getIconHeight()
{
Rectangle bounds = shape.getBounds();
return bounds.height;
}
/**
* {@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);
// Handle Icon position within the component and Shape translation
// (ie. X/Y positions in bounding rectangle are ignored)
Rectangle bounds = shape.getBounds();
g2d.translate(x - bounds.x, y - bounds.y);
// Fill the Shape with the specified Color
g2d.setColor(color);
g2d.fill( shape );
g2d.dispose();
}
}