001: /*
002: * $Id: PaintUtils.java,v 1.9 2006/06/28 20:20:37 joshy Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.util;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Container;
027: import java.awt.Font;
028: import java.awt.FontMetrics;
029: import java.awt.GradientPaint;
030: import java.awt.Graphics;
031: import java.awt.Graphics2D;
032: import java.awt.GraphicsConfiguration;
033: import java.awt.GraphicsDevice;
034: import java.awt.GraphicsEnvironment;
035: import java.awt.Image;
036: import java.awt.Paint;
037: import java.awt.Rectangle;
038: import java.awt.Transparency;
039: import java.awt.geom.Rectangle2D;
040: import java.awt.image.BufferedImage;
041: import java.io.IOException;
042: import java.net.URL;
043: import javax.imageio.ImageIO;
044: import javax.swing.BorderFactory;
045: import javax.swing.JComponent;
046: import javax.swing.JLabel;
047: import javax.swing.SwingConstants;
048: import javax.swing.border.BevelBorder;
049: import javax.swing.border.Border;
050:
051: /**
052: * A collection of utilties for painting visual effects.
053: *
054: * @author Mark Davidson
055: */
056: public class PaintUtils {
057: private static GraphicsConfiguration configuration = GraphicsEnvironment
058: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
059: .getDefaultConfiguration();
060:
061: // Utility methods.
062: private static Border defaultBorder = BorderFactory
063: .createBevelBorder(BevelBorder.RAISED);
064:
065: private PaintUtils() {
066: }
067:
068: public static Border getDefaultBorder() {
069: return defaultBorder;
070: }
071:
072: /**
073: * Returns the bounds that the text of a label will be drawn into.
074: * Takes into account the current font metrics.
075: */
076: public static Rectangle getTextBounds(Graphics g, JLabel label) {
077: FontMetrics fm = g.getFontMetrics();
078: Rectangle2D r2d = fm.getStringBounds(label.getText(), g);
079: Rectangle rect = r2d.getBounds();
080: int xOffset = 0;
081: switch (label.getHorizontalAlignment()) {
082: case SwingConstants.RIGHT:
083: case SwingConstants.TRAILING:
084: xOffset = label.getBounds().width - rect.width;
085: break;
086: case SwingConstants.CENTER:
087: xOffset = (label.getBounds().width - rect.width) / 2;
088: break;
089: default:
090: case SwingConstants.LEFT:
091: case SwingConstants.LEADING:
092: xOffset = 0;
093: break;
094: }
095: int yOffset = 0;
096: switch (label.getVerticalAlignment()) {
097: case SwingConstants.TOP:
098: yOffset = 0;
099: break;
100: case SwingConstants.CENTER:
101: yOffset = (label.getBounds().height - rect.height) / 2;
102: break;
103: case SwingConstants.BOTTOM:
104: yOffset = label.getBounds().height - rect.height;
105: break;
106: }
107: return new Rectangle(xOffset, yOffset, rect.width, rect.height);
108: }
109:
110: /**
111: * Paints a top to bottom gradient fill over the component bounds
112: * from color1 to color2.
113: */
114: public static void paintGradient(Graphics g, JComponent comp,
115: Color color1, Color color2) {
116: GradientPaint paint = new GradientPaint(0, 0, color1, 0, comp
117: .getHeight(), color2, true);
118: Graphics2D g2 = (Graphics2D) g;
119: Paint oldPaint = g2.getPaint();
120: g2.setPaint(paint);
121: g2.fillRect(0, 0, comp.getWidth(), comp.getHeight());
122: g2.setPaint(oldPaint);
123: }
124:
125: /**
126: * Sets the background color for a containment hierarchy.
127: */
128: public static void setBackgroundColor(Container cont, Color color) {
129: cont.setBackground(color);
130: Component[] children = cont.getComponents();
131: for (Component aChildren : children) {
132: if (aChildren instanceof Container) {
133: setBackgroundColor((Container) aChildren, color);
134: } else {
135: aChildren.setBackground(color);
136: }
137: }
138: }
139:
140: /**
141: * Sets the foreground color for a containment hierarchy.
142: */
143: public static void setForegroundColor(Container cont, Color color) {
144: cont.setForeground(color);
145: Component[] children = cont.getComponents();
146: for (Component aChildren : children) {
147: if (aChildren instanceof Container) {
148: setForegroundColor((Container) aChildren, color);
149: } else {
150: aChildren.setForeground(color);
151: }
152: }
153: }
154:
155: public static void setFont(Container cont, Font font) {
156: cont.setFont(font);
157: Component[] children = cont.getComponents();
158: for (Component aChildren : children) {
159: if (aChildren instanceof Container) {
160: setFont((Container) aChildren, font);
161: } else {
162: aChildren.setFont(font);
163: }
164: }
165: }
166:
167: /**
168: * @param width the width of the new BufferedImage
169: * @param height the height of the new BufferedImage
170: *
171: * @return Creates and returns a BufferedImage that is "compatible" with this machines
172: * video card and subsystem
173: */
174: public static BufferedImage createCompatibleImage(int width,
175: int height) {
176: GraphicsEnvironment environment = GraphicsEnvironment
177: .getLocalGraphicsEnvironment();
178: GraphicsDevice screenDevice = environment
179: .getDefaultScreenDevice();
180: GraphicsConfiguration configuration = screenDevice
181: .getDefaultConfiguration();
182: return configuration.createCompatibleImage(width, height);
183: }
184:
185: /**
186: * @param width the width of the new BufferedImage
187: * @param height the height of the new BufferedImage
188: * @param transparency one of the values in the Transparency interface
189: *
190: * @return Creates and returns a BufferedImage that is "compatible" with this machines
191: * video card and subsystem with the given Transparency.
192: */
193: public static BufferedImage createCompatibleImage(int width,
194: int height, int transparency) {
195: GraphicsEnvironment environment = GraphicsEnvironment
196: .getLocalGraphicsEnvironment();
197: GraphicsDevice screenDevice = environment
198: .getDefaultScreenDevice();
199: GraphicsConfiguration configuration = screenDevice
200: .getDefaultConfiguration();
201: return configuration.createCompatibleImage(width, height,
202: transparency);
203: }
204:
205: public static BufferedImage convertToBufferedImage(Image img) {
206: BufferedImage buff = createCompatibleImage(img.getWidth(null),
207: img.getHeight(null));
208: Graphics2D g2 = buff.createGraphics();
209: g2.drawImage(img, 0, 0, null);
210: g2.dispose();
211: return buff;
212: }
213:
214: public static BufferedImage loadCompatibleImage(URL resource)
215: throws IOException {
216: BufferedImage image = ImageIO.read(resource);
217: return toCompatibleImage(image);
218: }
219:
220: public static BufferedImage toCompatibleImage(BufferedImage image) {
221: BufferedImage compatibleImage = configuration
222: .createCompatibleImage(image.getWidth(), image
223: .getHeight(), Transparency.TRANSLUCENT);
224: Graphics g = compatibleImage.getGraphics();
225: g.drawImage(image, 0, 0, null);
226: g.dispose();
227: return compatibleImage;
228: }
229:
230: }
|