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: public class AWTUtilities {
033: static public Point computeDialogLocation(Dialog dialog, int w,
034: int h) {
035: Dimension scrnSz = dialog.getToolkit().getScreenSize();
036:
037: int x = (scrnSz.width - w) / 2;
038: int y = (scrnSz.height - h) / 3;
039:
040: return new Point(x, y);
041: }
042:
043: static public void constrain(Container container,
044: Component component, int fill, int anchor, int gx, int gy,
045: int gw, int gh, double wx, double wy) {
046: GridBagConstraints c = new GridBagConstraints();
047:
048: c.fill = fill;
049: c.anchor = anchor;
050: c.gridx = gx;
051: c.gridy = gy;
052: c.gridwidth = gw;
053: c.gridheight = gh;
054: c.weightx = wx;
055: c.weighty = wy;
056:
057: ((GridBagLayout) container.getLayout()).setConstraints(
058: component, c);
059:
060: container.add(component);
061: }
062:
063: static public void constrain(Container container,
064: Component component, int fill, int anchor, int gx, int gy,
065: int gw, int gh, double wx, double wy, int ipadx, int ipady) {
066: GridBagConstraints c = new GridBagConstraints();
067:
068: c.fill = fill;
069: c.anchor = anchor;
070: c.gridx = gx;
071: c.gridy = gy;
072: c.gridwidth = gw;
073: c.gridheight = gh;
074: c.weightx = wx;
075: c.weighty = wy;
076: c.ipadx = ipadx;
077: c.ipady = ipady;
078:
079: ((GridBagLayout) container.getLayout()).setConstraints(
080: component, c);
081:
082: container.add(component);
083: }
084:
085: static public Font getFont(String fontName) {
086: StringTokenizer toker = new StringTokenizer(fontName, "-",
087: false);
088:
089: String sName = "Helvetica";
090: String sStyle = "plain";
091: String sSize = "12";
092:
093: int numTokes = toker.countTokens();
094: boolean isok = true;
095:
096: try {
097: if (numTokes > 0) {
098: sName = toker.nextToken();
099:
100: if (numTokes == 2) {
101: sSize = toker.nextToken();
102: } else if (numTokes == 3) {
103: sStyle = toker.nextToken();
104: sSize = toker.nextToken();
105: }
106: }
107: } catch (Exception ex) {
108: System.err.println("Bad font specification '" + fontName
109: + "' - " + ex.getMessage());
110: return null;
111: }
112:
113: int style = (sStyle.compareTo("plain") == 0) ? Font.PLAIN
114: : ((sStyle.compareTo("bold") == 0) ? Font.BOLD
115: : ((sStyle.compareTo("italic") == 0) ? Font.ITALIC
116: : (Font.BOLD + Font.ITALIC)));
117:
118: int size = Integer.parseInt(sSize);
119:
120: return new Font(sName, style, size);
121: }
122:
123: public static Image getImageResource(String name)
124: throws java.io.IOException {
125: return com.ice.util.AWTUtilities.getImageResource(
126: com.ice.util.AWTUtilities.class, name);
127: }
128:
129: public static Image getImageResource(Class base, String name)
130: throws java.io.IOException {
131: Image result = null;
132:
133: Toolkit tk = Toolkit.getDefaultToolkit();
134:
135: URL imageURL = base.getResource(name);
136:
137: if (imageURL != null) {
138: result = tk.createImage((ImageProducer) imageURL
139: .getContent());
140: }
141:
142: return result;
143: }
144:
145: }
|