KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)BasicGraphicsUtils.java 1.61 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 javax.swing.*;
10 import java.awt.Component JavaDoc;
11 import java.awt.Color JavaDoc;
12 import java.awt.Dimension JavaDoc;
13 import java.awt.Font JavaDoc;
14 import java.awt.FontMetrics JavaDoc;
15 import java.awt.Graphics JavaDoc;
16 import java.awt.Insets JavaDoc;
17 import java.awt.Rectangle JavaDoc;
18 import java.awt.event.KeyEvent JavaDoc;
19 import com.sun.java.swing.SwingUtilities2;
20
21
22 /*
23  * @version 1.44 02/11/99
24  * @author Hans Muller
25  */

26
27 public class BasicGraphicsUtils
28 {
29
30     private static final Insets JavaDoc GROOVE_INSETS = new Insets JavaDoc(2, 2, 2, 2);
31     private static final Insets JavaDoc ETCHED_INSETS = new Insets JavaDoc(2, 2, 2, 2);
32
33     public static void drawEtchedRect(Graphics JavaDoc g, int x, int y, int w, int h,
34                                       Color JavaDoc shadow, Color JavaDoc darkShadow,
35                                       Color JavaDoc highlight, Color JavaDoc lightHighlight)
36     {
37         Color JavaDoc oldColor = g.getColor(); // Make no net change to g
38
g.translate(x, y);
39
40         g.setColor(shadow);
41         g.drawLine(0, 0, w-1, 0); // outer border, top
42
g.drawLine(0, 1, 0, h-2); // outer border, left
43

44         g.setColor(darkShadow);
45         g.drawLine(1, 1, w-3, 1); // inner border, top
46
g.drawLine(1, 2, 1, h-3); // inner border, left
47

48         g.setColor(lightHighlight);
49         g.drawLine(w-1, 0, w-1, h-1); // outer border, bottom
50
g.drawLine(0, h-1, w-1, h-1); // outer border, right
51

52         g.setColor(highlight);
53         g.drawLine(w-2, 1, w-2, h-3); // inner border, right
54
g.drawLine(1, h-2, w-2, h-2); // inner border, bottom
55

56         g.translate(-x, -y);
57         g.setColor(oldColor);
58     }
59
60
61     /**
62      * Returns the amount of space taken up by a border drawn by
63      * <code>drawEtchedRect()</code>
64      *
65      * @return the inset of an etched rect
66      */

67     public static Insets JavaDoc getEtchedInsets() {
68         return ETCHED_INSETS;
69     }
70
71
72     public static void drawGroove(Graphics JavaDoc g, int x, int y, int w, int h,
73                                   Color JavaDoc shadow, Color JavaDoc highlight)
74     {
75         Color JavaDoc oldColor = g.getColor(); // Make no net change to g
76
g.translate(x, y);
77
78         g.setColor(shadow);
79         g.drawRect(0, 0, w-2, h-2);
80
81         g.setColor(highlight);
82         g.drawLine(1, h-3, 1, 1);
83         g.drawLine(1, 1, w-3, 1);
84
85         g.drawLine(0, h-1, w-1, h-1);
86         g.drawLine(w-1, h-1, w-1, 0);
87
88         g.translate(-x, -y);
89         g.setColor(oldColor);
90     }
91
92     /**
93      * Returns the amount of space taken up by a border drawn by
94      * <code>drawGroove()</code>
95      *
96      * @return the inset of a groove border
97      */

98     public static Insets JavaDoc getGrooveInsets() {
99         return GROOVE_INSETS;
100     }
101
102
103     public static void drawBezel(Graphics JavaDoc g, int x, int y, int w, int h,
104                                  boolean isPressed, boolean isDefault,
105                                  Color JavaDoc shadow, Color JavaDoc darkShadow,
106                                  Color JavaDoc highlight, Color JavaDoc lightHighlight)
107     {
108         Color JavaDoc oldColor = g.getColor(); // Make no net change to g
109
g.translate(x, y);
110         
111         if (isPressed && isDefault) {
112             g.setColor(darkShadow);
113             g.drawRect(0, 0, w - 1, h - 1);
114             g.setColor(shadow);
115             g.drawRect(1, 1, w - 3, h - 3);
116         } else if (isPressed) {
117             drawLoweredBezel(g, x, y, w, h,
118                              shadow, darkShadow, highlight, lightHighlight);
119         } else if (isDefault) {
120             g.setColor(darkShadow);
121             g.drawRect(0, 0, w-1, h-1);
122
123             g.setColor(lightHighlight);
124             g.drawLine(1, 1, 1, h-3);
125             g.drawLine(2, 1, w-3, 1);
126
127             g.setColor(highlight);
128             g.drawLine(2, 2, 2, h-4);
129             g.drawLine(3, 2, w-4, 2);
130
131             g.setColor(shadow);
132             g.drawLine(2, h-3, w-3, h-3);
133             g.drawLine(w-3, 2, w-3, h-4);
134
135             g.setColor(darkShadow);
136             g.drawLine(1, h-2, w-2, h-2);
137             g.drawLine(w-2, h-2, w-2, 1);
138         } else {
139             g.setColor(lightHighlight);
140             g.drawLine(0, 0, 0, h-1);
141             g.drawLine(1, 0, w-2, 0);
142
143             g.setColor(highlight);
144             g.drawLine(1, 1, 1, h-3);
145             g.drawLine(2, 1, w-3, 1);
146
147             g.setColor(shadow);
148             g.drawLine(1, h-2, w-2, h-2);
149             g.drawLine(w-2, 1, w-2, h-3);
150
151             g.setColor(darkShadow);
152             g.drawLine(0, h-1, w-1, h-1);
153             g.drawLine(w-1, h-1, w-1, 0);
154         }
155     g.translate(-x, -y);
156     g.setColor(oldColor);
157     }
158
159     public static void drawLoweredBezel(Graphics JavaDoc g, int x, int y, int w, int h,
160                                         Color JavaDoc shadow, Color JavaDoc darkShadow,
161                                         Color JavaDoc highlight, Color JavaDoc lightHighlight) {
162         g.setColor(darkShadow);
163         g.drawLine(0, 0, 0, h-1);
164         g.drawLine(1, 0, w-2, 0);
165  
166         g.setColor(shadow);
167         g.drawLine(1, 1, 1, h-2);
168         g.drawLine(1, 1, w-3, 1);
169  
170         g.setColor(lightHighlight);
171         g.drawLine(0, h-1, w-1, h-1);
172         g.drawLine(w-1, h-1, w-1, 0);
173
174         g.setColor(highlight);
175         g.drawLine(1, h-2, w-2, h-2);
176         g.drawLine(w-2, h-2, w-2, 1);
177      }
178
179
180     /** Draw a string with the graphics <code>g</code> at location (x,y)
181      * just like <code>g.drawString</code> would.
182      * The first occurrence of <code>underlineChar</code>
183      * in text will be underlined. The matching algorithm is
184      * not case sensitive.
185      */

186     public static void drawString(Graphics JavaDoc g,String JavaDoc text,int underlinedChar,int x,int y) {
187         int index=-1;
188
189         if (underlinedChar != '\0') {
190             char uc = Character.toUpperCase((char)underlinedChar);
191             char lc = Character.toLowerCase((char)underlinedChar);
192             int uci = text.indexOf(uc);
193             int lci = text.indexOf(lc);
194
195             if(uci == -1) {
196                 index = lci;
197             }
198             else if(lci == -1) {
199                 index = uci;
200             }
201             else {
202                 index = (lci < uci) ? lci : uci;
203             }
204         }
205         drawStringUnderlineCharAt(g, text, index, x, y);
206     }
207
208     /**
209      * Draw a string with the graphics <code>g</code> at location
210      * (<code>x</code>, <code>y</code>)
211      * just like <code>g.drawString</code> would.
212      * The character at index <code>underlinedIndex</code>
213      * in text will be underlined. If <code>index</code> is beyond the
214      * bounds of <code>text</code> (including < 0), nothing will be
215      * underlined.
216      *
217      * @param g Graphics to draw with
218      * @param text String to draw
219      * @param underlinedIndex Index of character in text to underline
220      * @param x x coordinate to draw at
221      * @param y y coordinate to draw at
222      * @since 1.4
223      */

224     public static void drawStringUnderlineCharAt(Graphics JavaDoc g, String JavaDoc text,
225                            int underlinedIndex, int x,int y) {
226         SwingUtilities2.drawStringUnderlineCharAt(null, g, text,
227                                                   underlinedIndex, x, y);
228     }
229
230     public static void drawDashedRect(Graphics JavaDoc g,int x,int y,int width,int height) {
231         int vx,vy;
232
233         // draw upper and lower horizontal dashes
234
for (vx = x; vx < (x + width); vx+=2) {
235             g.fillRect(vx, y, 1, 1);
236             g.fillRect(vx, y + height-1, 1, 1);
237         }
238
239         // draw left and right vertical dashes
240
for (vy = y; vy < (y + height); vy+=2) {
241         g.fillRect(x, vy, 1, 1);
242             g.fillRect(x+width-1, vy, 1, 1);
243         }
244     }
245
246     public static Dimension JavaDoc getPreferredButtonSize(AbstractButton b, int textIconGap)
247     {
248         if(b.getComponentCount() > 0) {
249             return null;
250         }
251
252         Icon icon = (Icon) b.getIcon();
253         String JavaDoc text = b.getText();
254
255         Font JavaDoc font = b.getFont();
256         FontMetrics JavaDoc fm = b.getFontMetrics(font);
257           
258         Rectangle JavaDoc iconR = new Rectangle JavaDoc();
259         Rectangle JavaDoc textR = new Rectangle JavaDoc();
260         Rectangle JavaDoc viewR = new Rectangle JavaDoc(Short.MAX_VALUE, Short.MAX_VALUE);
261
262         SwingUtilities.layoutCompoundLabel(
263             (JComponent) b, fm, text, icon,
264             b.getVerticalAlignment(), b.getHorizontalAlignment(),
265             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
266             viewR, iconR, textR, (text == null ? 0 : textIconGap)
267         );
268
269         /* The preferred size of the button is the size of
270          * the text and icon rectangles plus the buttons insets.
271          */

272
273         Rectangle JavaDoc r = iconR.union(textR);
274
275         Insets JavaDoc insets = b.getInsets();
276         r.width += insets.left + insets.right;
277         r.height += insets.top + insets.bottom;
278
279         return r.getSize();
280     }
281     
282     /*
283      * Convenience function for determining ComponentOrientation. Helps us
284      * avoid having Munge directives throughout the code.
285      */

286     static boolean isLeftToRight( Component JavaDoc c ) {
287         return c.getComponentOrientation().isLeftToRight();
288     }
289 }
290
Popular Tags