KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicArrowButton


1 /*
2  * @(#)BasicArrowButton.java 1.26 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.basic;
8
9 import java.awt.Dimension JavaDoc;
10 import java.awt.Graphics JavaDoc;
11 import java.awt.Color JavaDoc;
12
13 import javax.swing.*;
14 import javax.swing.plaf.UIResource JavaDoc;
15
16 /**
17  * JButton object that draws a scaled Arrow in one of the cardinal directions.
18  * <p>
19  * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is
22  * appropriate for short term storage or RMI between applications running
23  * the same version of Swing. As of 1.4, support for long term storage
24  * of all JavaBeans<sup><font size="-2">TM</font></sup>
25  * has been added to the <code>java.beans</code> package.
26  * Please see {@link java.beans.XMLEncoder}.
27  *
28  * @version 1.26 12/19/03
29  * @author David Kloba
30  */

31 public class BasicArrowButton extends JButton implements SwingConstants
32 {
33         protected int direction;
34
35         private Color JavaDoc shadow;
36         private Color JavaDoc darkShadow;
37         private Color JavaDoc highlight;
38
39         public BasicArrowButton(int direction, Color JavaDoc background, Color JavaDoc shadow,
40              Color JavaDoc darkShadow, Color JavaDoc highlight) {
41         super();
42         setRequestFocusEnabled(false);
43             setDirection(direction);
44             setBackground(background);
45         this.shadow = shadow;
46         this.darkShadow = darkShadow;
47         this.highlight = highlight;
48     }
49
50         public BasicArrowButton(int direction) {
51         this(direction, UIManager.getColor("control"), UIManager.getColor("controlShadow"),
52          UIManager.getColor("controlDkShadow"), UIManager.getColor("controlLtHighlight"));
53         }
54
55         public int getDirection() { return direction; }
56
57         public void setDirection(int dir) { direction = dir; }
58
59     public void paint(Graphics JavaDoc g) {
60         Color JavaDoc origColor;
61         boolean isPressed, isEnabled;
62         int w, h, size;
63
64             w = getSize().width;
65             h = getSize().height;
66         origColor = g.getColor();
67         isPressed = getModel().isPressed();
68         isEnabled = isEnabled();
69
70             g.setColor(getBackground());
71             g.fillRect(1, 1, w-2, h-2);
72
73             /// Draw the proper Border
74
if (getBorder() != null && !(getBorder() instanceof UIResource JavaDoc)) {
75         paintBorder(g);
76         } else if (isPressed) {
77                 g.setColor(shadow);
78                 g.drawRect(0, 0, w-1, h-1);
79             } else {
80                 // Using the background color set above
81
g.drawLine(0, 0, 0, h-1);
82                 g.drawLine(1, 0, w-2, 0);
83
84                 g.setColor(highlight); // inner 3D border
85
g.drawLine(1, 1, 1, h-3);
86                 g.drawLine(2, 1, w-3, 1);
87
88                 g.setColor(shadow); // inner 3D border
89
g.drawLine(1, h-2, w-2, h-2);
90                 g.drawLine(w-2, 1, w-2, h-3);
91
92                 g.setColor(darkShadow); // black drop shadow __|
93
g.drawLine(0, h-1, w-1, h-1);
94                 g.drawLine(w-1, h-1, w-1, 0);
95             }
96
97             // If there's no room to draw arrow, bail
98
if(h < 5 || w < 5) {
99                 g.setColor(origColor);
100                 return;
101             }
102
103             if (isPressed) {
104                 g.translate(1, 1);
105             }
106
107             // Draw the arrow
108
size = Math.min((h - 4) / 3, (w - 4) / 3);
109             size = Math.max(size, 2);
110         paintTriangle(g, (w - size) / 2, (h - size) / 2,
111                 size, direction, isEnabled);
112
113             // Reset the Graphics back to it's original settings
114
if (isPressed) {
115                 g.translate(-1, -1);
116         }
117         g.setColor(origColor);
118
119         }
120
121         public Dimension JavaDoc getPreferredSize() {
122             return new Dimension JavaDoc(16, 16);
123         }
124
125         public Dimension JavaDoc getMinimumSize() {
126             return new Dimension JavaDoc(5, 5);
127         }
128
129         public Dimension JavaDoc getMaximumSize() {
130             return new Dimension JavaDoc(Integer.MAX_VALUE, Integer.MAX_VALUE);
131         }
132     
133         public boolean isFocusTraversable() {
134       return false;
135     }
136
137     public void paintTriangle(Graphics JavaDoc g, int x, int y, int size,
138                     int direction, boolean isEnabled) {
139         Color JavaDoc oldColor = g.getColor();
140         int mid, i, j;
141
142         j = 0;
143             size = Math.max(size, 2);
144         mid = (size / 2) - 1;
145     
146         g.translate(x, y);
147         if(isEnabled)
148         g.setColor(darkShadow);
149         else
150         g.setColor(shadow);
151
152             switch(direction) {
153             case NORTH:
154                 for(i = 0; i < size; i++) {
155                     g.drawLine(mid-i, i, mid+i, i);
156                 }
157                 if(!isEnabled) {
158                     g.setColor(highlight);
159                     g.drawLine(mid-i+2, i, mid+i, i);
160                 }
161                 break;
162             case SOUTH:
163                 if(!isEnabled) {
164                     g.translate(1, 1);
165                     g.setColor(highlight);
166                     for(i = size-1; i >= 0; i--) {
167                         g.drawLine(mid-i, j, mid+i, j);
168                         j++;
169                     }
170             g.translate(-1, -1);
171             g.setColor(shadow);
172         }
173         
174         j = 0;
175                 for(i = size-1; i >= 0; i--) {
176                     g.drawLine(mid-i, j, mid+i, j);
177                     j++;
178                 }
179                 break;
180             case WEST:
181                 for(i = 0; i < size; i++) {
182                     g.drawLine(i, mid-i, i, mid+i);
183                 }
184                 if(!isEnabled) {
185                     g.setColor(highlight);
186                     g.drawLine(i, mid-i+2, i, mid+i);
187                 }
188                 break;
189             case EAST:
190                 if(!isEnabled) {
191                     g.translate(1, 1);
192                     g.setColor(highlight);
193                     for(i = size-1; i >= 0; i--) {
194                         g.drawLine(j, mid-i, j, mid+i);
195                         j++;
196                     }
197             g.translate(-1, -1);
198             g.setColor(shadow);
199                 }
200
201         j = 0;
202                 for(i = size-1; i >= 0; i--) {
203                     g.drawLine(j, mid-i, j, mid+i);
204                     j++;
205                 }
206         break;
207             }
208         g.translate(-x, -y);
209         g.setColor(oldColor);
210     }
211     
212 }
213
214
Popular Tags