1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52:
53:
56: public class BasicArrowButton extends JButton implements SwingConstants
57: {
58:
59: private static int defaultSize = 12;
60:
61:
62: private static Polygon upIcon = new Polygon(new int[] { 0, 5, 9 },
63: new int[] { 7, 2, 7 }, 3);
64:
65:
66: private static Polygon downIcon = new Polygon(new int[] { 1, 5, 9 },
67: new int[] { 3, 7, 3 }, 3);
68:
69:
70: private static Polygon leftIcon = new Polygon(new int[] { 7, 3, 7 },
71: new int[] { 1, 5, 9 }, 3);
72:
73:
74: private static Polygon rightIcon = new Polygon(new int[] { 3, 7, 3 },
75: new int[] { 1, 5, 9 }, 3);
76:
77:
78: protected int direction;
79:
80:
85: transient Color shadow = Color.GRAY;
86:
87:
92: transient Color darkShadow = Color.DARK_GRAY;
93:
94:
98: transient Color highlight = Color.WHITE;
99:
100:
101: private transient Border buttonBorder = new Border()
102: {
103: public Insets getBorderInsets(Component c)
104: {
105: return new Insets(2, 2, 2, 2);
106: }
107:
108: public boolean isBorderOpaque()
109: {
110: return true;
111: }
112:
113: public void paintBorder(Component c, Graphics g, int x, int y, int w,
114: int h)
115: {
116: Color saved = g.getColor();
117: g.setColor(highlight);
118:
119: g.drawLine(x + 1, y + 1, x + w - 1, y + 1);
120: g.drawLine(x + 1, y + 1, x + 1, y + h - 1);
121:
122: g.setColor(shadow);
123:
124: g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1);
125: g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1);
126:
127: g.setColor(darkShadow);
128:
129: g.drawLine(x, y + h, x + w, y + h);
130: g.drawLine(x + w, y, x + w, y + h);
131:
132: g.setColor(saved);
133: }
134: };
135:
136:
141: public BasicArrowButton(int direction)
142: {
143: super();
144: setBorder(buttonBorder);
145: setDirection(direction);
146: }
147:
148:
158: public BasicArrowButton(int direction, Color background, Color shadow,
159: Color darkShadow, Color highlight)
160: {
161: this(direction);
162: setBackground(background);
163: this.shadow = shadow;
164: this.darkShadow = darkShadow;
165: this.highlight = highlight;
166: }
167:
168:
173: public boolean isFocusTraversable()
174: {
175: return false;
176: }
177:
178:
183: public int getDirection()
184: {
185: return direction;
186: }
187:
188:
193: public void setDirection(int dir)
194: {
195: this.direction = dir;
196: }
197:
198:
204: public void paint(Graphics g)
205: {
206: super.paint(g);
207: Insets insets = getInsets();
208: Rectangle bounds = getBounds();
209: int x = insets.left
210: + (bounds.width - insets.left - insets.right - defaultSize) / 2;
211: int y = insets.top
212: + (bounds.height - insets.left - insets.right - defaultSize) / 2;
213: paintTriangle(g, x, y, defaultSize, direction, isEnabled());
214: }
215:
216:
221: public Dimension getPreferredSize()
222: {
223: Insets insets = getInsets();
224: int w = defaultSize + insets.left + insets.right;
225: int h = defaultSize + insets.top + insets.bottom;
226:
227: return new Dimension(w, h);
228: }
229:
230:
235: public Dimension getMinimumSize()
236: {
237: return getPreferredSize();
238: }
239:
240:
245: public Dimension getMaximumSize()
246: {
247: return getPreferredSize();
248: }
249:
250:
261: public void paintTriangle(Graphics g, int x, int y, int size, int direction,
262: boolean isEnabled)
263: {
264: Polygon arrow = null;
265: switch (direction)
266: {
267: case NORTH:
268: arrow = upIcon;
269: break;
270: case SOUTH:
271: arrow = downIcon;
272: break;
273: case EAST:
274: case RIGHT:
275: arrow = rightIcon;
276: break;
277: case WEST:
278: case LEFT:
279: arrow = leftIcon;
280: break;
281: }
282:
283: int[] xPoints = arrow.xpoints;
284: int[] yPoints = arrow.ypoints;
285: int x1;
286: int y1;
287: int x2;
288: int y2;
289: x1 = y1 = x2 = y2 = 0;
290:
291: if (size != defaultSize)
292: {
293: float scale = size * 1f / defaultSize;
294: for (int i = 0; i < 3; i++)
295: {
296: xPoints[i] *= scale;
297: yPoints[i] *= scale;
298: }
299: }
300: g.translate(x, y);
301:
302: switch (direction)
303: {
304: case NORTH:
305: x1 = xPoints[0] + 2;
306: y1 = yPoints[0];
307: y2 = y1;
308: x2 = xPoints[2] - 1;
309: break;
310: case SOUTH:
311: x1 = xPoints[1];
312: y1 = yPoints[1] + 1;
313: x2 = xPoints[2] - 1;
314: y2 = yPoints[2];
315: break;
316: case LEFT:
317: case WEST:
318: x1 = xPoints[0] + 1;
319: y1 = yPoints[0] + 1;
320: x2 = x1;
321: y2 = yPoints[2] + 1;
322: break;
323: case RIGHT:
324: case EAST:
325: x1 = xPoints[2];
326: y1 = yPoints[2] + 1;
327: x2 = xPoints[1] - 1;
328: y2 = yPoints[1] + 1;
329: break;
330: }
331: Color saved = g.getColor();
332:
333: if (isEnabled)
334: {
335: g.setColor(Color.DARK_GRAY);
336:
337: if (arrow != null)
338: g.fillPolygon(xPoints, yPoints, 3);
339: }
340: else
341: {
342: g.setColor(Color.GRAY);
343: g.fillPolygon(xPoints, yPoints, 3);
344: g.setColor(Color.WHITE);
345: g.drawLine(x1, y1, x2, y2);
346: }
347: g.setColor(saved);
348: g.translate(-x, -y);
349: }
350: }