KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ice > util > AWTUtilities


1 /*
2 ** Tim Endres' utilities package.
3 ** Copyright (c) 1997 by Tim Endres
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package com.ice.util;
24
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.awt.image.*;
28 import java.net.URL JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.*;
31
32
33 public class
34 AWTUtilities
35     {
36     static public Point
37     computeDialogLocation( Dialog dialog, int w, int h )
38         {
39         Dimension scrnSz =
40             dialog.getToolkit().getScreenSize();
41
42         int x = (scrnSz.width - w) / 2;
43         int y = (scrnSz.height - h) / 3;
44         
45         return new Point( x, y );
46         }
47
48     static public void
49     constrain(
50             Container container, Component component,
51             int fill, int anchor,
52             int gx, int gy, int gw, int gh, double wx, double wy )
53         {
54         GridBagConstraints c =
55             new GridBagConstraints();
56
57         c.fill = fill;
58         c.anchor = anchor;
59         c.gridx = gx;
60         c.gridy = gy;
61         c.gridwidth = gw;
62         c.gridheight = gh;
63         c.weightx = wx;
64         c.weighty = wy;
65
66         ( (GridBagLayout)container.getLayout() ).
67             setConstraints( component, c );
68
69         container.add( component );
70         }
71
72     static public void
73     constrain(
74             Container container, Component component,
75             int fill, int anchor,
76             int gx, int gy, int gw, int gh,
77             double wx, double wy,
78             int ipadx, int ipady )
79         {
80         GridBagConstraints c =
81             new GridBagConstraints();
82
83         c.fill = fill;
84         c.anchor = anchor;
85         c.gridx = gx;
86         c.gridy = gy;
87         c.gridwidth = gw;
88         c.gridheight = gh;
89         c.weightx = wx;
90         c.weighty = wy;
91         c.ipadx = ipadx;
92         c.ipady = ipady;
93
94         ( (GridBagLayout)container.getLayout() ).
95             setConstraints( component, c );
96
97         container.add( component );
98         }
99
100     static public Font
101     getFont( String JavaDoc fontName )
102         {
103         StringTokenizer toker =
104             new StringTokenizer( fontName, "-", false );
105
106         String JavaDoc sName = "Helvetica";
107         String JavaDoc sStyle = "plain";
108         String JavaDoc sSize = "12";
109
110         int numTokes = toker.countTokens();
111         boolean isok = true;
112
113         try {
114             if ( numTokes > 0 )
115                 {
116                 sName = toker.nextToken();
117
118                 if ( numTokes == 2 )
119                     {
120                     sSize = toker.nextToken();
121                     }
122                 else if ( numTokes == 3 )
123                     {
124                     sStyle = toker.nextToken();
125                     sSize = toker.nextToken();
126                     }
127                 }
128             }
129         catch ( Exception JavaDoc ex )
130             {
131             System.err.println
132                 ( "Bad font specification '" + fontName + "' - "
133                     + ex.getMessage() );
134             return null;
135             }
136
137         int style =
138                   (sStyle.compareTo( "plain" ) == 0)
139                     ? Font.PLAIN :
140                 ( (sStyle.compareTo( "bold" ) == 0)
141                     ? Font.BOLD :
142                 ( (sStyle.compareTo( "italic" ) == 0)
143                     ? Font.ITALIC : (Font.BOLD + Font.ITALIC) ) );
144
145         int size = Integer.parseInt( sSize );
146
147         return new Font( sName, style, size );
148         }
149
150     public static Image
151     getImageResource( String JavaDoc name )
152         throws java.io.IOException JavaDoc
153         {
154         return
155             com.ice.util.AWTUtilities.getImageResource
156                 ( com.ice.util.AWTUtilities.class, name );
157         }
158
159     public static Image
160     getImageResource( Class JavaDoc base, String JavaDoc name )
161         throws java.io.IOException JavaDoc
162         {
163         Image result = null;
164
165         Toolkit tk = Toolkit.getDefaultToolkit();
166
167         URL JavaDoc imageURL = base.getResource( name );
168
169         if ( imageURL != null )
170             {
171             result = tk.createImage
172                 ( (ImageProducer) imageURL.getContent() );
173             }
174         
175         return result;
176         }
177
178     }
179
180
Popular Tags