1. /*
  2. * @(#)GraphicsEnvironment.java 1.55 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.image.BufferedImage;
  9. import java.util.Hashtable;
  10. import java.util.Locale;
  11. import java.util.Map;
  12. import java.io.InputStream;
  13. import sun.java2d.HeadlessGraphicsEnvironment;
  14. import sun.java2d.SunGraphicsEnvironment;
  15. /**
  16. *
  17. * The <code>GraphicsEnvironment</code> class describes the collection
  18. * of {@link GraphicsDevice} objects and {@link java.awt.Font} objects
  19. * available to a Java(tm) application on a particular platform.
  20. * The resources in this <code>GraphicsEnvironment</code> might be local
  21. * or on a remote machine. <code>GraphicsDevice</code> objects can be
  22. * screens, printers or image buffers and are the destination of
  23. * {@link Graphics2D} drawing methods. Each <code>GraphicsDevice</code>
  24. * has a number of {@link GraphicsConfiguration} objects associated with
  25. * it. These objects specify the different configurations in which the
  26. * <code>GraphicsDevice</code> can be used.
  27. * @see GraphicsDevice
  28. * @see GraphicsConfiguration
  29. * @version 1.55, 01/23/03
  30. */
  31. public abstract class GraphicsEnvironment {
  32. private static GraphicsEnvironment localEnv;
  33. /**
  34. * The headless state of the Toolkit and GraphicsEnvironment
  35. */
  36. private static Boolean headless;
  37. /**
  38. * This is an abstract class and cannot be instantiated directly.
  39. * Instances must be obtained from a suitable factory or query method.
  40. */
  41. protected GraphicsEnvironment() {
  42. }
  43. /**
  44. * Returns the local <code>GraphicsEnvironment</code>.
  45. * @return the local <code>GraphicsEnvironment</code>
  46. */
  47. public static synchronized GraphicsEnvironment getLocalGraphicsEnvironment() {
  48. if (localEnv == null) {
  49. String nm = (String) java.security.AccessController.doPrivileged
  50. (new sun.security.action.GetPropertyAction
  51. ("java.awt.graphicsenv", null));
  52. try {
  53. localEnv =
  54. (GraphicsEnvironment) Class.forName(nm).newInstance();
  55. if (isHeadless()) {
  56. localEnv = new HeadlessGraphicsEnvironment(localEnv);
  57. }
  58. } catch (ClassNotFoundException e) {
  59. throw new Error("Could not find class: "+nm);
  60. } catch (InstantiationException e) {
  61. throw new Error("Could not instantiate Graphics Environment: "
  62. + nm);
  63. } catch (IllegalAccessException e) {
  64. throw new Error ("Could not access Graphics Environment: "
  65. + nm);
  66. }
  67. }
  68. return localEnv;
  69. }
  70. /**
  71. * Tests whether or not a display, keyboard, and mouse can be
  72. * supported in this environment. If this method returns true,
  73. * a HeadlessException is thrown from areas of the Toolkit
  74. * and GraphicsEnvironment that are dependent on a display,
  75. * keyboard, or mouse.
  76. * @return <code>true</code> if this environment cannot support
  77. * a display, keyboard, and mouse; <code>false</code>
  78. * otherwise
  79. * @see java.awt.HeadlessException
  80. * @since 1.4
  81. */
  82. public static boolean isHeadless() {
  83. return getHeadlessProperty();
  84. }
  85. /**
  86. * @return the value of the property "java.awt.headless"
  87. * @since 1.4
  88. */
  89. private static boolean getHeadlessProperty() {
  90. if (headless == null) {
  91. String nm = (String)java.security.AccessController.doPrivileged(
  92. new sun.security.action.GetPropertyAction(
  93. "java.awt.headless", "false"));
  94. if (nm.equals("true")) {
  95. headless = Boolean.TRUE;
  96. } else {
  97. headless = Boolean.FALSE;
  98. }
  99. }
  100. return headless.booleanValue();
  101. }
  102. /**
  103. * Check for headless state and throw HeadlessException if headless
  104. * @since 1.4
  105. */
  106. static void checkHeadless() throws HeadlessException {
  107. if (isHeadless()) {
  108. throw new HeadlessException();
  109. }
  110. }
  111. /**
  112. * Returns whether or not a display, keyboard, and mouse can be
  113. * supported in this graphics environment. If this returns true,
  114. * <code>HeadlessException</code> will be thrown from areas of the
  115. * graphics environment that are dependent on a display, keyboard, or
  116. * mouse.
  117. * @return <code>true</code> if a display, keyboard, and mouse
  118. * can be supported in this environment; <code>false</code>
  119. * otherwise
  120. * @see java.awt.HeadlessException
  121. * @see #isHeadless
  122. * @since 1.4
  123. */
  124. public boolean isHeadlessInstance() {
  125. // By default (local graphics environment), simply check the
  126. // headless property.
  127. return getHeadlessProperty();
  128. }
  129. /**
  130. * Returns an array of all of the screen <code>GraphicsDevice</code>
  131. * objects.
  132. * @return an array containing all the <code>GraphicsDevice</code>
  133. * objects that represent screen devices
  134. * @exception HeadlessException if isHeadless() returns true
  135. * @see #isHeadless()
  136. */
  137. public abstract GraphicsDevice[] getScreenDevices()
  138. throws HeadlessException;
  139. /**
  140. * Returns the default screen <code>GraphicsDevice</code>.
  141. * @return the <code>GraphicsDevice</code> that represents the
  142. * default screen device
  143. * @exception HeadlessException if isHeadless() returns true
  144. * @see #isHeadless()
  145. */
  146. public abstract GraphicsDevice getDefaultScreenDevice()
  147. throws HeadlessException;
  148. /**
  149. * Returns a <code>Graphics2D</code> object for rendering into the
  150. * specified {@link BufferedImage}.
  151. * @param img the specified <code>BufferedImage</code>
  152. * @return a <code>Graphics2D</code> to be used for rendering into
  153. * the specified <code>BufferedImage</code>
  154. */
  155. public abstract Graphics2D createGraphics(BufferedImage img);
  156. /**
  157. * Returns an array containing a one-point size instance of all fonts
  158. * available in this <code>GraphicsEnvironment</code>. Typical usage
  159. * would be to allow a user to select a particular font. Then, the
  160. * application can size the font and set various font attributes by
  161. * calling the <code>deriveFont</code> method on the choosen instance.
  162. * <p>
  163. * This method provides for the application the most precise control
  164. * over which <code>Font</code> instance is used to render text.
  165. * If a font in this <code>GraphicsEnvironment</code> has multiple
  166. * programmable variations, only one
  167. * instance of that <code>Font</code> is returned in the array, and
  168. * other variations must be derived by the application.
  169. * <p>
  170. * If a font in this environment has multiple programmable variations,
  171. * such as Multiple-Master fonts, only one instance of that font is
  172. * returned in the <code>Font</code> array. The other variations
  173. * must be derived by the application.
  174. *
  175. * @return an array of <code>Font</code> objects
  176. * @see #getAvailableFontFamilyNames
  177. * @see java.awt.Font
  178. * @see java.awt.Font#deriveFont
  179. * @see java.awt.Font#getFontName
  180. * @since 1.2
  181. */
  182. public abstract Font[] getAllFonts();
  183. /**
  184. * Returns an array containing the names of all font families available
  185. * in this <code>GraphicsEnvironment</code>.
  186. * Typical usage would be to allow a user to select a particular family
  187. * name and allow the application to choose related variants of the
  188. * same family when the user specifies style attributes such
  189. * as Bold or Italic.
  190. * <p>
  191. * This method provides for the application some control over which
  192. * <code>Font</code> instance is used to render text, but allows the
  193. * <code>Font</code> object more flexibility in choosing its own best
  194. * match among multiple fonts in the same font family.
  195. *
  196. * @return an array of <code>String</code> containing names of font
  197. * families
  198. * @see #getAllFonts
  199. * @see java.awt.Font
  200. * @see java.awt.Font#getFamily
  201. * @since 1.2
  202. */
  203. public abstract String[] getAvailableFontFamilyNames();
  204. /**
  205. * Returns an array containing the localized names of all font families
  206. * available in this <code>GraphicsEnvironment</code>.
  207. * Typical usage would be to allow a user to select a particular family
  208. * name and allow the application to choose related variants of the
  209. * same family when the user specifies style attributes such
  210. * as Bold or Italic.
  211. * <p>
  212. * This method provides for the application some control over which
  213. * <code>Font</code> instance used to render text, but allows the
  214. * <code>Font</code> object more flexibility in choosing its own best
  215. * match among multiple fonts in the same font family.
  216. * If <code>l</code> is <code>null</code>, this method returns an
  217. * array containing all font family names available in this
  218. * <code>GraphicsEnvironment</code>.
  219. *
  220. * @param l a {@link Locale} object that represents a
  221. * particular geographical, political, or cultural region
  222. * @return an array of <code>String</code> objects containing names of
  223. * font families specific to the specified <code>Locale</code>
  224. * @see #getAllFonts
  225. * @see java.awt.Font
  226. * @see java.awt.Font#getFamily
  227. * @since 1.2
  228. */
  229. public abstract String[] getAvailableFontFamilyNames(Locale l);
  230. /**
  231. * Returns the Point where Windows should be centered.
  232. * It is recommended that centered Windows be checked to ensure they fit
  233. * within the available display area using getMaximumWindowBounds().
  234. * @return the point where Windows should be centered
  235. *
  236. * @exception HeadlessException if isHeadless() returns true
  237. * @see #getMaximumWindowBounds
  238. * @since 1.4
  239. */
  240. public Point getCenterPoint() throws HeadlessException {
  241. // Default implementation: return the center of the usable bounds of the
  242. // default screen device.
  243. Rectangle usableBounds =
  244. SunGraphicsEnvironment.getUsableBounds(getDefaultScreenDevice());
  245. return new Point((usableBounds.width / 2) + usableBounds.x,
  246. (usableBounds.height / 2) + usableBounds.y);
  247. }
  248. /**
  249. * Returns the maximum bounds for centered Windows.
  250. * These bounds account for objects in the native windowing system such as
  251. * task bars and menu bars. The returned bounds will reside on a single
  252. * display with one exception: on multi-screen systems where Windows should
  253. * be centered across all displays, this method returns the bounds of the
  254. * entire display area.
  255. * <p>
  256. * To get the usable bounds of a single display, use
  257. * <code>GraphicsConfiguration.getBounds()</code> and
  258. * <code>Toolkit.getScreenInsets()</code>.
  259. * @return the maximum bounds for centered Windows
  260. *
  261. * @exception HeadlessException if isHeadless() returns true
  262. * @see #getCenterPoint
  263. * @see GraphicsConfiguration#getBounds
  264. * @see Toolkit#getScreenInsets
  265. * @since 1.4
  266. */
  267. public Rectangle getMaximumWindowBounds() throws HeadlessException {
  268. // Default implementation: return the usable bounds of the default screen
  269. // device. This is correct for Microsoft Windows and non-Xinerama X11.
  270. return SunGraphicsEnvironment.getUsableBounds(getDefaultScreenDevice());
  271. }
  272. }