GraphicsEnvironment.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* GraphicsEnvironment.java -- information about the graphics environment
  2. Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import gnu.java.awt.ClasspathToolkit;
  33. import gnu.classpath.SystemProperties;
  34. import java.awt.image.BufferedImage;
  35. import java.util.Locale;
  36. /**
  37. * This descibes the collection of GraphicsDevice and Font objects available
  38. * on a given platform. The resources might be local or remote, and specify
  39. * the valid configurations for displaying graphics.
  40. *
  41. * @author Eric Blake (ebb9@email.byu.edu)
  42. * @see GraphicsDevice
  43. * @see GraphicsConfiguration
  44. * @since 1.4
  45. * @status updated to 1.4
  46. */
  47. public abstract class GraphicsEnvironment
  48. {
  49. private static GraphicsEnvironment localGraphicsEnvironment;
  50. /**
  51. * The environment must be obtained from a factory or query method, hence
  52. * this constructor is protected.
  53. */
  54. protected GraphicsEnvironment()
  55. {
  56. }
  57. /**
  58. * Returns the local graphics environment. If the java.awt.graphicsenv
  59. * system property is set, it instantiates the specified class,
  60. * otherwise it assume that the awt toolkit is a ClasspathToolkit
  61. * and delegates to it to create the instance.
  62. *
  63. * @return the local environment
  64. */
  65. public static GraphicsEnvironment getLocalGraphicsEnvironment()
  66. {
  67. if (localGraphicsEnvironment != null)
  68. return localGraphicsEnvironment;
  69. String graphicsenv = SystemProperties.getProperty("java.awt.graphicsenv",
  70. null);
  71. if (graphicsenv != null)
  72. {
  73. try
  74. {
  75. // We intentionally use the bootstrap class loader.
  76. localGraphicsEnvironment = (GraphicsEnvironment)
  77. Class.forName(graphicsenv).newInstance();
  78. return localGraphicsEnvironment;
  79. }
  80. catch (Exception x)
  81. {
  82. throw (InternalError)
  83. new InternalError("Unable to instantiate java.awt.graphicsenv")
  84. .initCause(x);
  85. }
  86. }
  87. else
  88. {
  89. ClasspathToolkit tk;
  90. tk = ((ClasspathToolkit) Toolkit.getDefaultToolkit());
  91. localGraphicsEnvironment = tk.getLocalGraphicsEnvironment();
  92. return localGraphicsEnvironment;
  93. }
  94. }
  95. /**
  96. * Check if the local environment is headless, meaning that it does not
  97. * support a display, keyboard, or mouse. Many methods in the Abstract
  98. * Windows Toolkit (java.awt) throw a {@link HeadlessException} if this
  99. * returns true.
  100. *
  101. * This method returns true if the java.awt.headless property is set
  102. * to "true".
  103. *
  104. * @return true if the environment is headless, meaning that graphics are
  105. * unsupported
  106. * @since 1.4
  107. */
  108. public static boolean isHeadless()
  109. {
  110. String headless = SystemProperties.getProperty("java.awt.headless", null);
  111. return "true".equalsIgnoreCase(headless);
  112. }
  113. /**
  114. * Check if the given environment is headless, meaning that it does not
  115. * support a display, keyboard, or mouse. Many methods in the Abstract
  116. * Windows Toolkit (java.awt) throw a {@link HeadlessException} if this
  117. * returns true. This default implementation returns isHeadless(), so
  118. * subclasses need only override it if they differ.
  119. *
  120. * @return true if the environment is headless, meaning that graphics are
  121. * unsupported
  122. * @since 1.4
  123. */
  124. public boolean isHeadlessInstance()
  125. {
  126. return isHeadless();
  127. }
  128. /**
  129. * Get an array of all the GraphicsDevice objects.
  130. *
  131. * @return the available graphics devices, may be 0 length
  132. * @throws HeadlessException if the environment is headless
  133. */
  134. public abstract GraphicsDevice[] getScreenDevices();
  135. /**
  136. * Get the default screen GraphicsDevice object.
  137. *
  138. * @return the default screen device
  139. * @throws HeadlessException if the environment is headless
  140. */
  141. public abstract GraphicsDevice getDefaultScreenDevice();
  142. /**
  143. * Return a Graphics2D object which will render into the specified image.
  144. *
  145. * @param image the image to render into
  146. * @return the object that renders into the image
  147. */
  148. public abstract Graphics2D createGraphics(BufferedImage image);
  149. /**
  150. * Returns an array of the one-point size fonts available in this
  151. * environment. From there, the user can select the font and derive the
  152. * correct one of proper size and attributes, using <code>deriveFont</code>.
  153. * Only one master version of each font appears in this array; if a font
  154. * can be derived from another, it must be created in that way.
  155. *
  156. * @return the array of available fonts
  157. * @see #getAvailableFontFamilyNames()
  158. * @see Font#deriveFont(int, float)
  159. * @since 1.2
  160. */
  161. public abstract Font[] getAllFonts();
  162. /**
  163. * Returns an array of the font family names available in this environment.
  164. * This allows flexibility in choosing the style of font, while still letting
  165. * the Font class decide its best match.
  166. *
  167. * @return the array of available font families
  168. * @see #getAllFonts()
  169. * @see Font#getFamily()
  170. * @since 1.2
  171. */
  172. public abstract String[] getAvailableFontFamilyNames();
  173. /**
  174. * Returns an array of the font family names available in this environment,
  175. * localized to the current Locale if l is non-null. This allows
  176. * flexibility in choosing the style of font, while still letting the Font
  177. * class decide its best match.
  178. *
  179. * @param l the locale to use
  180. * @return the array of available font families, localized
  181. * @see #getAllFonts()
  182. * @see Font#getFamily()
  183. * @since 1.2
  184. */
  185. public abstract String[] getAvailableFontFamilyNames(Locale l);
  186. /**
  187. * Returns the point where a window should be centered. You should probably
  188. * also check that the window fits within the screen bounds. The default
  189. * simply returns the center of the maximum window bounds; subclasses should
  190. * override this if native objects (like scrollbars) make that off-centered.
  191. *
  192. * @return the centering point
  193. * @throws HeadlessException if the environment is headless
  194. * @see #getMaximumWindowBounds()
  195. * @since 1.4
  196. */
  197. public Point getCenterPoint()
  198. {
  199. Rectangle r = getMaximumWindowBounds();
  200. return new Point(r.x + r.width / 2, r.y + r.height / 2);
  201. }
  202. /**
  203. * Returns the maximum bounds for a centered window object. The default
  204. * implementation simply returns the bounds of the default configuration
  205. * of the default screen; subclasses should override this to if native
  206. * objects (like scrollbars) reduce what is truly available. Also,
  207. * subclasses should override this if the window should be centered across
  208. * a multi-screen display.
  209. *
  210. * @return the maximum window bounds
  211. * @throws HeadlessException if the environment is headless
  212. * @see #getCenterPoint()
  213. * @see GraphicsConfiguration#getBounds()
  214. * @see Toolkit#getScreenInsets(GraphicsConfiguration)
  215. * @since 1.4
  216. */
  217. public Rectangle getMaximumWindowBounds()
  218. {
  219. return getDefaultScreenDevice().getDefaultConfiguration().getBounds();
  220. }
  221. } // class GraphicsEnvironment