1 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 ; 29 import java.io.IOException ; 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 fontName ) 102 { 103 StringTokenizer toker = 104 new StringTokenizer( fontName, "-", false ); 105 106 String sName = "Helvetica"; 107 String sStyle = "plain"; 108 String 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 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 name ) 152 throws java.io.IOException 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 base, String name ) 161 throws java.io.IOException 162 { 163 Image result = null; 164 165 Toolkit tk = Toolkit.getDefaultToolkit(); 166 167 URL 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 |