-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDualIcon.java
More file actions
193 lines (178 loc) · 6.14 KB
/
DualIcon.java
File metadata and controls
193 lines (178 loc) · 6.14 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
/**
* @(#)DualIcon.java 1.0 03/18/09
*/
package darrylbu.renderer;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.SwingConstants;
/**
* A composite Icon class used to compose two Icon objects into a single Icon
* by painting the icons in turn at the precomputed offsets. For example,
* this class may be used to add a custom icon to a component like JCheckBox
* or JRadioButton, in addition to the default icon provided by the
* Look and Feel.
*
* @version 1.0 03/11/09
* @author Darryl
*/
public class DualIcon implements Icon, SwingConstants {
Icon icon1;
Icon icon2;
private int width;
private int height;
private int icon1HOffset;
private int icon1VOffset;
private int icon2HOffset;
private int icon2VOffset;
private int iconIconGap;
/**
* Creates a <CODE>DualIcon</CODE> with the specified icons, the default
* horizontal and vertical positioning and default gap.
* <CODE>icon2</CODE> is positioned to the right of <CODE>icon1</CODE>
* with a 4 pixel gap, and the vertical centers of the icons are aligned.
*
* @param icon1 the first icon
* @param icon2 the second icon
*/
public DualIcon(Icon icon1, Icon icon2) {
this(icon1, icon2, RIGHT, CENTER, 4);
}
/**
* Creates a <CODE>DualIcon</CODE> with the specified icons, the specified
* horizontal and vertical positioning and the specified gap.
*
* @param icon1 the first icon
* @param icon2 the second icon
* @param horizontalPosition of the second icon relative to the first.
* <BR>
* One of the following values:
* <ul>
* <li>{@code SwingConstants.LEFT}
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.RIGHT}
* </ul>
* <P>
* @param verticalPosition of the second icon relative to the first.
* <BR>
* One of the following values:
* <ul>
* <li>{@code SwingConstants.TOP}
* <li>{@code SwingConstants.CENTER}
* <li>{@code SwingConstants.BOTTOM}
* </ul>
* <P>
* @param iconIconGap the gap between the icons in pizels, ignored if the
* horizontalPosition and verticalPosition are both
* <CODE>SwingConstants.CENTER</CODE>.
*/
public DualIcon(Icon icon1, Icon icon2,
int horizontalPosition, int verticalPosition, int iconIconGap) {
if (icon1 == null || icon2 == null) {
throw new NullPointerException("Icons cannot be null");
}
horizontalPosition = checkHorizontalKey(horizontalPosition,
"horizontalPosition");
verticalPosition = checkVerticalKey(verticalPosition,
"verticalPosition");
this.icon1 = icon1;
this.icon2 = icon2;
this.iconIconGap = iconIconGap;
if (horizontalPosition == CENTER) {
width = Math.max(icon1.getIconWidth(),
icon2.getIconWidth());
} else {
width = icon1.getIconWidth() + iconIconGap +
icon2.getIconWidth();
}
if (verticalPosition == CENTER || horizontalPosition != CENTER) {
height = Math.max(icon1.getIconHeight(),
icon2.getIconHeight());
} else {
height = icon1.getIconHeight() + iconIconGap +
icon2.getIconHeight();
}
switch (horizontalPosition) {
case LEFT:
icon1HOffset = 0;
icon2HOffset = icon1.getIconWidth() + iconIconGap;
break;
case CENTER:
icon1HOffset = (width - icon1.getIconWidth()) / 2;
icon2HOffset = (width - icon2.getIconWidth()) / 2;
break;
case RIGHT:
icon1HOffset = icon2.getIconWidth() + iconIconGap;
icon2HOffset = 0;
break;
}
if (verticalPosition == CENTER) {
icon1VOffset = (height - icon1.getIconHeight()) / 2;
icon2VOffset = (height - icon2.getIconHeight()) / 2;
} else {
if (horizontalPosition == CENTER) {
icon1VOffset = verticalPosition == TOP
? 0 : icon2.getIconHeight() + iconIconGap;
icon2VOffset = verticalPosition == TOP
? icon1.getIconHeight() + iconIconGap : 0;
} else {
icon1VOffset = verticalPosition == TOP
? 0 : height - icon1.getIconHeight();
icon2VOffset = verticalPosition == TOP
? 0 : height - icon2.getIconHeight();
}
}
}
/**
* Paints the icons of this compound icon at the specified location
* with the precomputed offsets.
*
* @param c The component to which the icon is painted, which may be used
* to get properties useful for painting, e.g. the foreground or background
* color or selection status.
* @param g the graphics context
* @param x the X coordinate of the compound icon's top-left corner
* @param y the Y coordinate of the compound icon's top-left corner
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
icon1.paintIcon(c, g,
x + icon1HOffset, y + icon1VOffset);
icon2.paintIcon(c, g,
x + icon2HOffset, y + icon2VOffset);
}
/**
* Gets the width of the bounding rectangle of this
* <CODE>DualIcon</CODE>.
*
* @return the width in pixels
*/
@Override
public int getIconWidth() {
return width;
}
/**
* Gets the height of the the bounding rectangle of this
* <CODE>DualIcon</CODE>.
*
* @return the height in pixels
*/
@Override
public int getIconHeight() {
return height;
}
private int checkHorizontalKey(int key, String exception) {
if ((key == LEFT) || (key == CENTER) || (key == RIGHT)) {
return key;
} else {
throw new IllegalArgumentException(exception);
}
}
private int checkVerticalKey(int key, String exception) {
if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
return key;
} else {
throw new IllegalArgumentException(exception);
}
}
}