Source Code Cross Referenced for AWTUtilities.java in  » Source-Control » jcvsweb » com » ice » 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 » Source Control » jcvsweb » com.ice.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         ** Tim Endres' utilities package.
003:         ** Copyright (c) 1997 by Tim Endres
004:         ** 
005:         ** This program is free software.
006:         ** 
007:         ** You may redistribute it and/or modify it under the terms of the GNU
008:         ** General Public License as published by the Free Software Foundation.
009:         ** Version 2 of the license should be included with this distribution in
010:         ** the file LICENSE, as well as License.html. If the license is not
011:         ** included	with this distribution, you may find a copy at the FSF web
012:         ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013:         ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014:         **
015:         ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016:         ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017:         ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018:         ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019:         ** REDISTRIBUTION OF THIS SOFTWARE. 
020:         ** 
021:         */
022:
023:        package com.ice.util;
024:
025:        import java.awt.*;
026:        import java.awt.event.*;
027:        import java.awt.image.*;
028:        import java.net.URL;
029:        import java.io.IOException;
030:        import java.util.*;
031:
032:        /**
033:         * This is a class that contains useful utility functions related
034:         * to the Java AWT package.
035:         */
036:
037:        public class AWTUtilities {
038:            static public Point centerDialogInParent(Dialog dialog,
039:                    Component parent) {
040:                Point parLoc = parent.getLocationOnScreen();
041:
042:                Dimension parSz = parent.getSize();
043:                Dimension dlgSz = dialog.getSize();
044:
045:                int x = parLoc.x + (parSz.width - dlgSz.width) / 2;
046:                int y = parLoc.y + (parSz.height - dlgSz.height) / 3;
047:
048:                return new Point(x, y);
049:            }
050:
051:            static public Point computeDialogLocation(Dialog dialog, int w,
052:                    int h) {
053:                Dimension scrnSz = dialog.getToolkit().getScreenSize();
054:
055:                int x = (scrnSz.width - w) / 2;
056:                int y = (scrnSz.height - h) / 3;
057:
058:                return new Point(x, y);
059:            }
060:
061:            static public Point computeDialogLocation(Dialog dialog) {
062:                Dimension dlgSz = dialog.getSize();
063:                Dimension scrnSz = dialog.getToolkit().getScreenSize();
064:
065:                int x = (scrnSz.width - dlgSz.width) / 2;
066:                int y = (scrnSz.height - dlgSz.height) / 3;
067:
068:                return new Point(x, y);
069:            }
070:
071:            static public Point computeDialogLocation(Dialog dialog,
072:                    Component rel) {
073:                Dimension dlgSz = dialog.getSize();
074:                Dimension scrnSz = dialog.getToolkit().getScreenSize();
075:
076:                int x = (scrnSz.width - dlgSz.width) / 2;
077:                int y = (scrnSz.height - dlgSz.height) / 3;
078:
079:                if (rel != null) {
080:                    Dimension relSz = rel.getSize();
081:                    Point loc = rel.getLocationOnScreen();
082:
083:                    x = loc.x + ((relSz.width - dlgSz.width) / 2);
084:
085:                    y = loc.y + ((relSz.height - dlgSz.height) / 2);
086:                }
087:
088:                if (x < 0)
089:                    x = 0;
090:                if (y < 0)
091:                    y = 0;
092:
093:                return new Point(x, y);
094:            }
095:
096:            static public void constrain(Container container,
097:                    Component component, int fill, int anchor, int gx, int gy,
098:                    int gw, int gh, double wx, double wy) {
099:                GridBagConstraints c = new GridBagConstraints();
100:
101:                c.fill = fill;
102:                c.anchor = anchor;
103:                c.gridx = gx;
104:                c.gridy = gy;
105:                c.gridwidth = gw;
106:                c.gridheight = gh;
107:                c.weightx = wx;
108:                c.weighty = wy;
109:
110:                ((GridBagLayout) container.getLayout()).setConstraints(
111:                        component, c);
112:
113:                container.add(component);
114:            }
115:
116:            static public void constrain(Container container,
117:                    Component component, int fill, int anchor, int gx, int gy,
118:                    int gw, int gh, double wx, double wy, Insets inset) {
119:                GridBagConstraints c = new GridBagConstraints();
120:
121:                c.fill = fill;
122:                c.anchor = anchor;
123:                c.gridx = gx;
124:                c.gridy = gy;
125:                c.gridwidth = gw;
126:                c.gridheight = gh;
127:                c.weightx = wx;
128:                c.weighty = wy;
129:                c.insets = inset;
130:
131:                ((GridBagLayout) container.getLayout()).setConstraints(
132:                        component, c);
133:
134:                container.add(component);
135:            }
136:
137:            static public void constrain(Container container,
138:                    Component component, int fill, int anchor, int gx, int gy,
139:                    int gw, int gh, double wx, double wy, int ipadx, int ipady) {
140:                GridBagConstraints c = new GridBagConstraints();
141:
142:                c.fill = fill;
143:                c.anchor = anchor;
144:                c.gridx = gx;
145:                c.gridy = gy;
146:                c.gridwidth = gw;
147:                c.gridheight = gh;
148:                c.weightx = wx;
149:                c.weighty = wy;
150:                c.ipadx = ipadx;
151:                c.ipady = ipady;
152:
153:                ((GridBagLayout) container.getLayout()).setConstraints(
154:                        component, c);
155:
156:                container.add(component);
157:            }
158:
159:            static public Font getFont(String fontName) {
160:                StringTokenizer toker = new StringTokenizer(fontName, "-",
161:                        false);
162:
163:                String sName = "Helvetica";
164:                String sStyle = "plain";
165:                String sSize = "12";
166:
167:                int numTokes = toker.countTokens();
168:                boolean isok = true;
169:
170:                try {
171:                    if (numTokes > 0) {
172:                        sName = toker.nextToken();
173:
174:                        if (numTokes == 2) {
175:                            sSize = toker.nextToken();
176:                        } else if (numTokes == 3) {
177:                            sStyle = toker.nextToken();
178:                            sSize = toker.nextToken();
179:                        }
180:                    }
181:                } catch (Exception ex) {
182:                    System.err.println("Bad font specification '" + fontName
183:                            + "' - " + ex.getMessage());
184:                    return null;
185:                }
186:
187:                int style = (sStyle.equalsIgnoreCase("plain")) ? Font.PLAIN
188:                        : ((sStyle.equalsIgnoreCase("bold")) ? Font.BOLD
189:                                : ((sStyle.equalsIgnoreCase("italic")) ? Font.ITALIC
190:                                        : (Font.BOLD + Font.ITALIC)));
191:
192:                int size = Integer.parseInt(sSize);
193:
194:                return new Font(sName, style, size);
195:            }
196:
197:            // The subtlety in getResource() is that it uses the
198:            // Class loader of the class used to get the rousource.
199:            // This means that if you want to load a resource from
200:            // your JAR file, then you better use a class in the
201:            // JAR file.
202:
203:            public static Image getImageResource(String name)
204:                    throws java.io.IOException {
205:                return AWTUtilities.getImageResource(AWTUtilities.class, name);
206:            }
207:
208:            public static Image getImageResource(Class base, String name)
209:                    throws java.io.IOException {
210:                Image result = null;
211:
212:                URL imageURL = base.getResource(name);
213:
214:                if (imageURL != null) {
215:                    Toolkit tk = Toolkit.getDefaultToolkit();
216:
217:                    result = tk.createImage((ImageProducer) imageURL
218:                            .getContent());
219:                }
220:
221:                return result;
222:            }
223:
224:            public static Image getSystemImageResource(String name)
225:                    throws java.io.IOException {
226:                Image result = null;
227:
228:                URL imageURL = ClassLoader.getSystemResource(name);
229:                if (imageURL != null) {
230:                    Toolkit tk = Toolkit.getDefaultToolkit();
231:
232:                    result = tk.createImage((ImageProducer) imageURL
233:                            .getContent());
234:                }
235:
236:                return result;
237:            }
238:
239:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.