KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > PaintUtils


1 /*
2  * $Id: PaintUtils.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.*;
11 import java.awt.geom.Rectangle2D JavaDoc;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.swing.*;
20
21 import javax.swing.border.BevelBorder JavaDoc;
22 import javax.swing.border.Border JavaDoc;
23
24 /**
25  * A collection of utilties for painting visual effects.
26  *
27  * @author Mark Davidson
28  */

29 class PaintUtils {
30
31     // Utility methods.
32

33     private static Border JavaDoc defaultBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED);
34
35     public static Border JavaDoc getDefaultBorder() {
36     return defaultBorder;
37     }
38
39     /**
40      * Returns the bounds that the text of a label will be drawn into.
41      * Takes into account the current font metrics.
42      */

43     public static Rectangle getTextBounds(Graphics g, JLabel label) {
44
45     FontMetrics fm = g.getFontMetrics();
46     Rectangle2D JavaDoc r2d = fm.getStringBounds(label.getText(), g);
47     Rectangle rect = r2d.getBounds();
48
49     int xOffset = 0;
50     switch (label.getHorizontalAlignment()) {
51     case SwingConstants.RIGHT:
52     case SwingConstants.TRAILING:
53         xOffset = label.getBounds().width - rect.width;
54         break;
55
56     case SwingConstants.CENTER:
57         xOffset = (label.getBounds().width - rect.width)/2;
58         break;
59
60     default:
61     case SwingConstants.LEFT:
62     case SwingConstants.LEADING:
63         xOffset = 0;
64         break;
65     }
66
67     int yOffset = 0;
68     switch (label.getVerticalAlignment()) {
69     case SwingConstants.TOP:
70         yOffset = 0;
71         break;
72     case SwingConstants.CENTER:
73         yOffset = (label.getBounds().height - rect.height)/2;
74         break;
75     case SwingConstants.BOTTOM:
76         yOffset = label.getBounds().height - rect.height;
77         break;
78     }
79     return new Rectangle(xOffset, yOffset, rect.width, rect.height);
80     }
81
82     /**
83      * Paints a top to bottom gradient fill over the component bounds
84      * from color1 to color2.
85      */

86     public static void paintGradient(Graphics g, JComponent comp,
87                      Color color1, Color color2) {
88     GradientPaint paint = new GradientPaint(0, 0, color1,
89                         0, comp.getHeight(), color2,
90                         true);
91     Graphics2D g2 = (Graphics2D)g;
92     Paint oldPaint = g2.getPaint();
93
94     g2.setPaint(paint);
95     g2.fillRect(0, 0, comp.getWidth(), comp.getHeight());
96     g2.setPaint(oldPaint);
97     }
98
99     /**
100      * Sets the background color for a containment hierarchy.
101      */

102     public static void setBackgroundColor(Container cont, Color color) {
103     cont.setBackground(color);
104     Component[] children = cont.getComponents();
105     for (int i = 0; i < children.length; i++) {
106         if (children[i] instanceof Container) {
107         setBackgroundColor((Container)children[i], color);
108         } else {
109         children[i].setBackground(color);
110         }
111     }
112     }
113
114     /**
115      * Sets the foreground color for a containment hierarchy.
116      */

117     public static void setForegroundColor(Container cont, Color color) {
118     cont.setForeground(color);
119     Component[] children = cont.getComponents();
120     for (int i = 0; i < children.length; i++) {
121         if (children[i] instanceof Container) {
122         setForegroundColor((Container)children[i], color);
123         } else {
124         children[i].setForeground(color);
125         }
126     }
127     }
128
129     public static void setFont(Container cont, Font font) {
130     cont.setFont(font);
131     Component[] children = cont.getComponents();
132     for (int i = 0; i < children.length; i++) {
133         if (children[i] instanceof Container) {
134         setFont((Container)children[i], font);
135         } else {
136         children[i].setFont(font);
137         }
138     }
139     }
140 }
141
Popular Tags