Source Code Cross Referenced for PaintUtils.java in  » Swing-Library » swingx » org » jdesktop » swingx » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » swingx » org.jdesktop.swingx.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.