1 7 8 9 package java.awt; 10 11 import java.awt.image.BufferedImage ; 12 import java.util.Hashtable ; 13 import java.util.Locale ; 14 import java.util.Map ; 15 import java.io.InputStream ; 16 import sun.java2d.HeadlessGraphicsEnvironment; 17 import sun.java2d.SunGraphicsEnvironment; 18 19 35 36 public abstract class GraphicsEnvironment { 37 private static GraphicsEnvironment localEnv; 38 39 42 private static Boolean headless; 43 44 47 private static Boolean defaultHeadless; 48 49 53 protected GraphicsEnvironment() { 54 } 55 56 60 public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() { 61 if (localEnv == null) { 62 String nm = (String ) java.security.AccessController.doPrivileged 63 (new sun.security.action.GetPropertyAction 64 ("java.awt.graphicsenv", null)); 65 66 try { 67 localEnv = 69 (GraphicsEnvironment ) Class.forName(nm).newInstance(); 70 if (isHeadless()) { 73 localEnv = new HeadlessGraphicsEnvironment(localEnv); 74 } 75 } catch (ClassNotFoundException e) { 76 throw new Error ("Could not find class: "+nm); 77 } catch (InstantiationException e) { 78 throw new Error ("Could not instantiate Graphics Environment: " 79 + nm); 80 } catch (IllegalAccessException e) { 81 throw new Error ("Could not access Graphics Environment: " 82 + nm); 83 } 84 } 85 86 return localEnv; 87 } 88 89 101 public static boolean isHeadless() { 102 return getHeadlessProperty(); 103 } 104 105 110 static String getHeadlessMessage() { 111 if (headless == null) { 112 getHeadlessProperty(); } 114 return defaultHeadless != Boolean.TRUE ? null : 115 "\nNo X11 DISPLAY variable was set, " + 116 "but this program performed an operation which requires it."; 117 } 118 119 123 private static boolean getHeadlessProperty() { 124 if (headless == null) { 125 java.security.AccessController.doPrivileged( 126 new java.security.PrivilegedAction () { 127 public Object run() { 128 String nm = System.getProperty("java.awt.headless"); 129 130 if (nm == null) { 131 132 if (System.getProperty("javaplugin.version") != null) { 133 headless = defaultHeadless = Boolean.FALSE; 134 } else { 135 String osName = System.getProperty("os.name"); 136 headless = defaultHeadless = 137 Boolean.valueOf(("Linux".equals(osName) || "SunOS".equals(osName)) && 138 (System.getenv("DISPLAY") == null)); 139 } 140 } else if (nm.equals("true")) { 141 headless = Boolean.TRUE; 142 } else { 143 headless = Boolean.FALSE; 144 } 145 return null; 146 } 147 } 148 ); 149 } 150 return headless.booleanValue(); 151 } 152 153 157 static void checkHeadless() throws HeadlessException { 158 if (isHeadless()) { 159 throw new HeadlessException (); 160 } 161 } 162 163 176 public boolean isHeadlessInstance() { 177 return getHeadlessProperty(); 180 } 181 182 190 public abstract GraphicsDevice [] getScreenDevices() 191 throws HeadlessException ; 192 193 200 public abstract GraphicsDevice getDefaultScreenDevice() 201 throws HeadlessException ; 202 203 210 public abstract Graphics2D createGraphics(BufferedImage img); 211 212 238 public abstract Font [] getAllFonts(); 239 240 259 public abstract String [] getAvailableFontFamilyNames(); 260 261 283 public abstract String [] getAvailableFontFamilyNames(Locale l); 284 285 305 public void preferLocaleFonts() { 306 sun.font.FontManager.preferLocaleFonts(); 307 } 308 309 325 public void preferProportionalFonts() { 326 sun.font.FontManager.preferProportionalFonts(); 327 } 328 329 339 public Point getCenterPoint() throws HeadlessException { 340 Rectangle usableBounds = 343 SunGraphicsEnvironment.getUsableBounds(getDefaultScreenDevice()); 344 return new Point ((usableBounds.width / 2) + usableBounds.x, 345 (usableBounds.height / 2) + usableBounds.y); 346 } 347 348 367 public Rectangle getMaximumWindowBounds() throws HeadlessException { 368 return SunGraphicsEnvironment.getUsableBounds(getDefaultScreenDevice()); 371 } 372 } 373 374 | Popular Tags |