-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompoundIcon.java
More file actions
289 lines (257 loc) · 7.86 KB
/
Copy pathCompoundIcon.java
File metadata and controls
289 lines (257 loc) · 7.86 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import java.awt.*;
import javax.swing.*;
/**
* The CompoundIcon will paint two, or more, Icons as a single Icon. The
* Icons are painted in the order in which they are added.
*
* @author http://www.camick.com/java/source/CompoundIcon.java
*
* The Icons are layed out on the specified axis:
* <ul>
* <li>X-Axis (horizontally)
* <li>Y-Axis (vertically)
* <li>Z-Axis (stacked)
* </ul>
*
*/
public class CompoundIcon implements Icon
{
public enum Axis
{
X_AXIS,
Y_AXIS,
Z_AXIS;
}
public final static float TOP = 0.0f;
public final static float LEFT = 0.0f;
public final static float CENTER = 0.5f;
public final static float BOTTOM = 1.0f;
public final static float RIGHT = 1.0f;
private Icon[] icons;
private Axis axis;
private int gap;
private float alignmentX = CENTER;
private float alignmentY = CENTER;
/**
* Convenience contructor for creating a CompoundIcon where the
* icons are layed out on on the X-AXIS, the gap is 0 and the
* X/Y alignments will default to CENTER.
*
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Icon... icons)
{
this(Axis.X_AXIS, icons);
}
/**
* Convenience contructor for creating a CompoundIcon where the
* gap is 0 and the X/Y alignments will default to CENTER.
*
* @param axis the axis used to lay out the icons for painting.
* Must be one of the Axis enums: X_AXIS, Y_AXIS, Z_Axis.
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Axis axis, Icon... icons)
{
this(axis, 0, icons);
}
/**
* Convenience contructor for creating a CompoundIcon where the
* X/Y alignments will default to CENTER.
*
* @param axis the axis used to lay out the icons for painting
* Must be one of the Axis enums: X_AXIS, Y_AXIS, Z_Axis.
* @param gap the gap between the icons
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Axis axis, int gap, Icon... icons)
{
this(axis, gap, CENTER, CENTER, icons);
}
/**
* Create a CompoundIcon specifying all the properties.
*
* @param axis the axis used to lay out the icons for painting
* Must be one of the Axis enums: X_AXIS, Y_AXIS, Z_Axis.
* @param gap the gap between the icons
* @param alignmentX the X alignment of the icons. Common values are
* LEFT, CENTER, RIGHT. Can be any value between 0.0 and 1.0
* @param alignmentY the Y alignment of the icons. Common values are
* TOP, CENTER, BOTTOM. Can be any value between 0.0 and 1.0
* @param icons the Icons to be painted as part of the CompoundIcon
*/
public CompoundIcon(Axis axis, int gap, float alignmentX, float alignmentY, Icon... icons)
{
this.axis = axis;
this.gap = gap;
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
for (int i = 0; i < icons.length; i++)
{
if (icons[i] == null)
{
String message = "Icon (" + i + ") cannot be null";
throw new IllegalArgumentException( message );
}
}
this.icons = icons;
}
/**
* Get the Axis along which each icon is painted.
*
* @return the Axis
*/
public Axis getAxis()
{
return axis;
}
/**
* Get the gap between each icon
*
* @return the gap in pixels
*/
public int getGap()
{
return gap;
}
/**
* Get the alignment of the icon on the x-axis
*
* @return the alignment
*/
public float getAlignmentX()
{
return alignmentX;
}
/**
* Get the alignment of the icon on the y-axis
*
* @return the alignment
*/
public float getAlignmentY()
{
return alignmentY;
}
/**
* Get the number of Icons contained in this CompoundIcon.
*
* @return the total number of Icons
*/
public int getIconCount()
{
return icons.length;
}
/**
* Get the Icon at the specified index.
*
* @param index the index of the Icon to be returned
* @return the Icon at the specifed index
* @exception IndexOutOfBoundsException if the index is out of range
*/
public Icon getIcon(int index)
{
return icons[ index ];
}
//
// Implement the Icon Interface
//
/**
* Gets the width of this icon.
*
* @return the width of the icon in pixels.
*/
@Override
public int getIconWidth()
{
int width = 0;
// Add the width of all Icons while also including the gap
if (axis == Axis.X_AXIS)
{
width += (icons.length - 1) * gap;
for (Icon icon : icons)
width += icon.getIconWidth();
}
else // Just find the maximum width
{
for (Icon icon : icons)
width = Math.max(width, icon.getIconWidth());
}
return width;
}
/**
* Gets the height of this icon.
*
* @return the height of the icon in pixels.
*/
@Override
public int getIconHeight()
{
int height = 0;
// Add the height of all Icons while also including the gap
if (axis == Axis.Y_AXIS)
{
height += (icons.length - 1) * gap;
for (Icon icon : icons)
height += icon.getIconHeight();
}
else // Just find the maximum height
{
for (Icon icon : icons)
height = Math.max(height, icon.getIconHeight());
}
return height;
}
/**
* 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)
{
if (axis == Axis.X_AXIS)
{
int height = getIconHeight();
for (Icon icon : icons)
{
int iconY = getOffset(height, icon.getIconHeight(), alignmentY);
icon.paintIcon(c, g, x, y + iconY);
x += icon.getIconWidth() + gap;
}
}
else if (axis == Axis.Y_AXIS)
{
int width = getIconWidth();
for (Icon icon : icons)
{
int iconX = getOffset(width, icon.getIconWidth(), alignmentX);
icon.paintIcon(c, g, x + iconX, y);
y += icon.getIconHeight() + gap;
}
}
else // must be Z_AXIS
{
int width = getIconWidth();
int height = getIconHeight();
for (Icon icon : icons)
{
int iconX = getOffset(width, icon.getIconWidth(), alignmentX);
int iconY = getOffset(height, icon.getIconHeight(), alignmentY);
icon.paintIcon(c, g, x + iconX, y + iconY);
}
}
}
/*
* When the icon value is smaller than the maximum value of all icons the
* icon needs to be aligned appropriately. Calculate the offset to be used
* when painting the icon to achieve the proper alignment.
*/
private int getOffset(int maxValue, int iconValue, float alignment)
{
float offset = (maxValue - iconValue) * alignment;
return Math.round(offset);
}
}