001    /*
002     * $Id: PaintUtils.java,v 1.8 2006/05/14 15:55:54 dmouse 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.geom.Rectangle2D;
039    import java.awt.image.BufferedImage;
040    import javax.swing.BorderFactory;
041    import javax.swing.JComponent;
042    import javax.swing.JLabel;
043    import javax.swing.SwingConstants;
044    import javax.swing.border.BevelBorder;
045    import javax.swing.border.Border;
046    
047    /**
048     * A collection of utilties for painting visual effects.
049     *
050     * @author Mark Davidson
051     */
052    public class PaintUtils {
053    
054        //  Utility methods. 
055        private static Border defaultBorder =
056                BorderFactory.createBevelBorder(BevelBorder.RAISED);
057    
058        private PaintUtils() {
059        }
060    
061        public static Border getDefaultBorder() {
062            return defaultBorder;
063        }
064    
065        /**
066         * Returns the bounds that the text of a label will be drawn into.
067         * Takes into account the current font metrics.
068         */
069        public static Rectangle getTextBounds(Graphics g, JLabel label) {
070            FontMetrics fm = g.getFontMetrics();
071            Rectangle2D r2d = fm.getStringBounds(label.getText(), g);
072            Rectangle rect = r2d.getBounds();
073            int xOffset = 0;
074            switch (label.getHorizontalAlignment()) {
075                case SwingConstants.RIGHT:
076                case SwingConstants.TRAILING:
077                    xOffset = label.getBounds().width - rect.width;
078                    break;
079                case SwingConstants.CENTER:
080                    xOffset = (label.getBounds().width - rect.width) / 2;
081                    break;
082                default:
083                case SwingConstants.LEFT:
084                case SwingConstants.LEADING:
085                    xOffset = 0;
086                    break;
087            }
088            int yOffset = 0;
089            switch (label.getVerticalAlignment()) {
090                case SwingConstants.TOP:
091                    yOffset = 0;
092                    break;
093                case SwingConstants.CENTER:
094                    yOffset = (label.getBounds().height - rect.height) / 2;
095                    break;
096                case SwingConstants.BOTTOM:
097                    yOffset = label.getBounds().height - rect.height;
098                    break;
099            }
100            return new Rectangle(xOffset, yOffset, rect.width, rect.height);
101        }
102    
103        /**
104         * Paints a top to bottom gradient fill over the component bounds
105         * from color1 to color2.
106         */
107        public static void paintGradient(Graphics g, JComponent comp,
108                                         Color color1, Color color2) {
109            GradientPaint paint = new GradientPaint(0, 0, color1,
110                                                    0, comp.getHeight(), color2,
111                                                    true);
112            Graphics2D g2 = (Graphics2D) g;
113            Paint oldPaint = g2.getPaint();
114            g2.setPaint(paint);
115            g2.fillRect(0, 0, comp.getWidth(), comp.getHeight());
116            g2.setPaint(oldPaint);
117        }
118    
119        /**
120         * Sets the background color for a containment hierarchy.
121         */
122        public static void setBackgroundColor(Container cont, Color color) {
123            cont.setBackground(color);
124            Component[] children = cont.getComponents();
125            for (Component aChildren : children) {
126                if (aChildren instanceof Container) {
127                    setBackgroundColor((Container) aChildren, color);
128                } else {
129                    aChildren.setBackground(color);
130                }
131            }
132        }
133    
134        /**
135         * Sets the foreground color for a containment hierarchy.
136         */
137        public static void setForegroundColor(Container cont, Color color) {
138            cont.setForeground(color);
139            Component[] children = cont.getComponents();
140            for (Component aChildren : children) {
141                if (aChildren instanceof Container) {
142                    setForegroundColor((Container) aChildren, color);
143                } else {
144                    aChildren.setForeground(color);
145                }
146            }
147        }
148    
149        public static void setFont(Container cont, Font font) {
150            cont.setFont(font);
151            Component[] children = cont.getComponents();
152            for (Component aChildren : children) {
153                if (aChildren instanceof Container) {
154                    setFont((Container) aChildren, font);
155                } else {
156                    aChildren.setFont(font);
157                }
158            }
159        }
160    
161        /**
162         * @param width  the width of the new BufferedImage
163         * @param height the height of the new BufferedImage
164         *
165         * @return Creates and returns a BufferedImage that is "compatible" with this machines
166         *         video card and subsystem
167         */
168        public static BufferedImage createCompatibleImage(int width, int height) {
169            GraphicsEnvironment environment =
170                    GraphicsEnvironment.getLocalGraphicsEnvironment();
171            GraphicsDevice screenDevice = environment.getDefaultScreenDevice();
172            GraphicsConfiguration configuration =
173                    screenDevice.getDefaultConfiguration();
174            return configuration.createCompatibleImage(width, height);
175        }
176    
177        /**
178         * @param width         the width of the new BufferedImage
179         * @param height        the height of the new BufferedImage
180         * @param transparency  one of the values in the Transparency interface
181         *
182         * @return Creates and returns a BufferedImage that is "compatible" with this machines
183         *         video card and subsystem with the given Transparency.
184         */
185        public static BufferedImage createCompatibleImage(int width, int height,
186                                                          int transparency) {
187            GraphicsEnvironment environment =
188                    GraphicsEnvironment.getLocalGraphicsEnvironment();
189            GraphicsDevice screenDevice = environment.getDefaultScreenDevice();
190            GraphicsConfiguration configuration =
191                    screenDevice.getDefaultConfiguration();
192            return configuration.createCompatibleImage(width, height, transparency);
193        }
194        
195        public static BufferedImage convertToBufferedImage(Image img) {
196            BufferedImage buff = createCompatibleImage(img.getWidth(null),img.getHeight(null));
197            Graphics2D g2 = buff.createGraphics();
198            g2.drawImage(img,0,0,null);
199            g2.dispose();
200            return buff;
201        }
202    }