1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components;
21
22 import java.awt.Container;
23 import java.awt.Dimension;
24 import java.awt.Graphics;
25 import java.awt.Graphics2D;
26 import java.awt.GridLayout;
27 import java.awt.Insets;
28 import java.awt.event.WindowAdapter;
29 import java.awt.event.WindowEvent;
30
31 import javax.swing.JApplet;
32 import javax.swing.JFrame;
33 import javax.swing.JPanel;
34
35 import com.cosylab.gui.components.util.PaintHelper;
36
37
38
39
40
41
42
43
44
45
46 public class ArrowButton extends SimpleButton
47 {
48 private static final long serialVersionUID = -2014851617632168689L;
49
50 public static final int PREFERRED_SIZE = 20;
51
52
53 public static int UP = 0;
54
55
56 public static int DOWN = 1;
57
58
59 public static int LEFT = 2;
60
61
62 public static int RIGHT = 3;
63 private Insets insets;
64 private int orientation;
65
66
67
68
69
70
71 public ArrowButton(int newOrientation)
72 {
73 super();
74
75 if (newOrientation != UP && newOrientation != DOWN
76 && newOrientation != LEFT && newOrientation != RIGHT) {
77 throw (new IllegalArgumentException());
78 }
79
80 orientation = newOrientation;
81 setMinimumSize(new Dimension(10, 10));
82 setPreferredSize(new Dimension(PREFERRED_SIZE, PREFERRED_SIZE));
83 insets = null;
84 setActionMode(CHAIN_ACTION_MODE);
85 }
86
87
88
89
90
91
92 public void setArrowInsets(Insets newInsets)
93 {
94 Insets oldInsets = insets;
95 insets = newInsets;
96 firePropertyChange("arrowInsets", oldInsets, newInsets);
97
98 if (insets != oldInsets) {
99 repaint();
100 }
101 }
102
103
104
105
106
107
108 public Insets getArrowInsets()
109 {
110 return insets;
111 }
112
113
114
115
116
117
118 public void paintComponent(Graphics g)
119 {
120 ((Graphics2D)g).addRenderingHints(PaintHelper.getAntialiasingHints());
121 super.paintComponent(g);
122
123 int maxHeight = getBounds().height;
124 int maxWidth = getBounds().width;
125 int top;
126 int bottom;
127 int left;
128 int right;
129
130 if (insets == null) {
131 top = Math.min(maxWidth, maxHeight) / 4;
132 bottom = Math.min(maxWidth, maxHeight) / 4;
133 left = Math.min(maxWidth, maxHeight) / 4;
134 right = Math.min(maxWidth, maxHeight) / 4;
135 } else {
136 top = insets.top;
137 bottom = insets.bottom;
138 left = insets.left;
139 right = insets.right;
140 }
141
142 g.setColor(getForeground());
143
144 if (orientation == DOWN) {
145 int[] xs = { left, maxWidth / 2, maxWidth - right };
146 int[] ys = { top, maxHeight - bottom, top };
147 g.fillPolygon(xs, ys, 3);
148 } else if (orientation == UP) {
149 int[] xs = { left, maxWidth / 2, maxWidth - right };
150 int[] ys = { maxHeight - bottom, top, maxHeight - bottom };
151 g.fillPolygon(xs, ys, 3);
152 } else if (orientation == RIGHT) {
153 int[] ys = { top, maxHeight / 2, maxHeight - bottom };
154 int[] xs = { left, maxWidth - right, left };
155 g.fillPolygon(xs, ys, 3);
156 } else if (orientation == LEFT) {
157 int[] ys = { top, maxHeight / 2, maxHeight - bottom };
158 int[] xs = { maxWidth - right, left, maxWidth - right };
159 g.fillPolygon(xs, ys, 3);
160 }
161 }
162
163
164
165
166
167
168 public int getOrientation()
169 {
170 return orientation;
171 }
172
173
174
175
176
177
178 public void setOrientation(int newOrientation)
179 {
180 int oldOrientation = orientation;
181 orientation = newOrientation;
182 firePropertyChange("orientation", oldOrientation, newOrientation);
183
184 if (oldOrientation != newOrientation) {
185 repaint();
186 }
187 }
188
189
190
191
192
193
194 public static void main(String[] args)
195 {
196 JApplet applet = new JApplet() {
197
198 private static final long serialVersionUID = 1L;
199
200 public void init()
201 {
202 Container cp = this.getContentPane();
203 JPanel panel = new JPanel();
204 panel.setLayout(new GridLayout(1, 0));
205 panel.add(new ArrowButton(ArrowButton.UP));
206 panel.add(new ArrowButton(ArrowButton.DOWN));
207 panel.add(new ArrowButton(ArrowButton.LEFT));
208 panel.add(new ArrowButton(ArrowButton.RIGHT));
209 cp.add(panel);
210 }
211 };
212
213 JFrame frame = new JFrame("Arrow Button Demo");
214 frame.getContentPane().add(applet);
215 frame.setSize(300, 400);
216 frame.addWindowListener(new WindowAdapter() {
217 public void windowClosing(WindowEvent e)
218 {
219 System.exit(0);
220 }
221 });
222 applet.init();
223 applet.start();
224 frame.setVisible(true);
225 }
226 }
227
228