1. /*
  2. * @(#)GraphicsDevice.java 1.30 03/12/19
  3. *
  4. * Copyright 2004 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.ColorModel;
  9. /**
  10. * The <code>GraphicsDevice</code> class describes the graphics devices
  11. * that might be available in a particular graphics environment. These
  12. * include screen and printer devices. Note that there can be many screens
  13. * and many printers in an instance of {@link GraphicsEnvironment}. Each
  14. * graphics device has one or more {@link GraphicsConfiguration} objects
  15. * associated with it. These objects specify the different configurations
  16. * in which the <code>GraphicsDevice</code> can be used.
  17. * <p>
  18. * In a multi-screen environment, the <code>GraphicsConfiguration</code>
  19. * objects can be used to render components on multiple screens. The
  20. * following code sample demonstrates how to create a <code>JFrame</code>
  21. * object for each <code>GraphicsConfiguration</code> on each screen
  22. * device in the <code>GraphicsEnvironment</code>:
  23. * <pre>
  24. * GraphicsEnvironment ge = GraphicsEnvironment.
  25. * getLocalGraphicsEnvironment();
  26. * GraphicsDevice[] gs = ge.getScreenDevices();
  27. * for (int j = 0; j < gs.length; j++) {
  28. * GraphicsDevice gd = gs[j];
  29. * GraphicsConfiguration[] gc =
  30. * gd.getConfigurations();
  31. * for (int i=0; i < gc.length; i++) {
  32. * JFrame f = new
  33. * JFrame(gs[j].getDefaultConfiguration());
  34. * Canvas c = new Canvas(gc[i]);
  35. * Rectangle gcBounds = gc[i].getBounds();
  36. * int xoffs = gcBounds.x;
  37. * int yoffs = gcBounds.y;
  38. * f.getContentPane().add(c);
  39. * f.setLocation((i*50)+xoffs, (i*60)+yoffs);
  40. * f.show();
  41. * }
  42. * }
  43. * </pre>
  44. * @see GraphicsEnvironment
  45. * @see GraphicsConfiguration
  46. * @version 1.30, 12/19/03
  47. */
  48. public abstract class GraphicsDevice {
  49. private Window fullScreenWindow;
  50. private Rectangle windowedModeBounds;
  51. /**
  52. * This is an abstract class that cannot be instantiated directly.
  53. * Instances must be obtained from a suitable factory or query method.
  54. * @see GraphicsEnvironment#getScreenDevices
  55. * @see GraphicsEnvironment#getDefaultScreenDevice
  56. * @see GraphicsConfiguration#getDevice
  57. */
  58. protected GraphicsDevice() {
  59. }
  60. /**
  61. * Device is a raster screen.
  62. */
  63. public final static int TYPE_RASTER_SCREEN = 0;
  64. /**
  65. * Device is a printer.
  66. */
  67. public final static int TYPE_PRINTER = 1;
  68. /**
  69. * Device is an image buffer. This buffer can reside in device
  70. * or system memory but it is not physically viewable by the user.
  71. */
  72. public final static int TYPE_IMAGE_BUFFER = 2;
  73. /**
  74. * Returns the type of this <code>GraphicsDevice</code>.
  75. * @return the type of this <code>GraphicsDevice</code>, which can
  76. * either be TYPE_RASTER_SCREEN, TYPE_PRINTER or TYPE_IMAGE_BUFFER.
  77. * @see #TYPE_RASTER_SCREEN
  78. * @see #TYPE_PRINTER
  79. * @see #TYPE_IMAGE_BUFFER
  80. */
  81. public abstract int getType();
  82. /**
  83. * Returns the identification string associated with this
  84. * <code>GraphicsDevice</code>.
  85. * <p>
  86. * A particular program might use more than one
  87. * <code>GraphicsDevice</code> in a <code>GraphicsEnvironment</code>.
  88. * This method returns a <code>String</code> identifying a
  89. * particular <code>GraphicsDevice</code> in the local
  90. * <code>GraphicsEnvironment</code>. Although there is
  91. * no public method to set this <code>String</code>, a programmer can
  92. * use the <code>String</code> for debugging purposes. Vendors of
  93. * the Java<sup><font size=-2>TM</font></sup> Runtime Environment can
  94. * format the return value of the <code>String</code>. To determine
  95. * how to interpret the value of the <code>String</code>, contact the
  96. * vendor of your Java Runtime. To find out who the vendor is, from
  97. * your program, call the
  98. * {@link System#getProperty(String) getProperty} method of the
  99. * System class with "java.vendor".
  100. * @return a <code>String</code> that is the identification
  101. * of this <code>GraphicsDevice</code>.
  102. */
  103. public abstract String getIDstring();
  104. /**
  105. * Returns all of the <code>GraphicsConfiguration</code>
  106. * objects associated with this <code>GraphicsDevice</code>.
  107. * @return an array of <code>GraphicsConfiguration</code>
  108. * objects that are associated with this
  109. * <code>GraphicsDevice</code>.
  110. */
  111. public abstract GraphicsConfiguration[] getConfigurations();
  112. /**
  113. * Returns the default <code>GraphicsConfiguration</code>
  114. * associated with this <code>GraphicsDevice</code>.
  115. * @return the default <code>GraphicsConfiguration</code>
  116. * of this <code>GraphicsDevice</code>.
  117. */
  118. public abstract GraphicsConfiguration getDefaultConfiguration();
  119. /**
  120. * Returns the "best" configuration possible that passes the
  121. * criteria defined in the {@link GraphicsConfigTemplate}.
  122. * @param gct the <code>GraphicsConfigTemplate</code> object
  123. * used to obtain a valid <code>GraphicsConfiguration</code>
  124. * @return a <code>GraphicsConfiguration</code> that passes
  125. * the criteria defined in the specified
  126. * <code>GraphicsConfigTemplate</code>.
  127. * @see GraphicsConfigTemplate
  128. */
  129. public GraphicsConfiguration
  130. getBestConfiguration(GraphicsConfigTemplate gct) {
  131. GraphicsConfiguration[] configs = getConfigurations();
  132. return gct.getBestConfiguration(configs);
  133. }
  134. /**
  135. * Returns <code>true</code> if this <code>GraphicsDevice</code>
  136. * supports full-screen exclusive mode.
  137. * @return whether full-screen exclusive mode is available for
  138. * this graphics device
  139. * @since 1.4
  140. */
  141. public boolean isFullScreenSupported() {
  142. return false;
  143. }
  144. /**
  145. * Enter full-screen mode, or return to windowed mode.
  146. * <p>
  147. * If <code>isFullScreenSupported</code> returns <code>true</code>, full
  148. * screen mode is considered to be <i>exclusive</i>, which implies:
  149. * <ul>
  150. * <li>Windows cannot overlap the full-screen window. All other application
  151. * windows will always appear beneath the full-screen window in the Z-order.
  152. * <li>Input method windows are disabled. It is advisable to call
  153. * <code>Component.enableInputMethods(false)</code> to make a component
  154. * a non-client of the input method framework.
  155. * </ul>
  156. * <p>
  157. * If <code>isFullScreenSupported</code> returns
  158. * <code>false</code>, full-screen exclusive mode is simulated by resizing
  159. * the window to the size of the screen and positioning it at (0,0).
  160. * <p>
  161. * When entering full-screen exclusive mode, if the window to be used as the
  162. * full-screen window is not visible, this method will make it visible.
  163. * It will remain visible when returning to windowed mode.
  164. * <p>
  165. * When returning to windowed mode from an exclusive full-screen window, any
  166. * display changes made by calling <code>setDisplayMode</code> are
  167. * automatically restored to their original state.
  168. *
  169. * @param w a window to use as the full-screen window; <code>null</code>
  170. * if returning to windowed mode. Some platforms expect the
  171. * fullscreen window to be a top-level component (i.e., a Frame);
  172. * therefore it is preferable to use a Frame here rather than a
  173. * Window.
  174. * @see #isFullScreenSupported
  175. * @see #getFullScreenWindow
  176. * @see #setDisplayMode
  177. * @see Component#enableInputMethods
  178. * @see Component#setVisible
  179. * @since 1.4
  180. */
  181. public void setFullScreenWindow(Window w) {
  182. // Get display mode before changing the full screen window
  183. DisplayMode dm;
  184. if (w == null) {
  185. dm = null;
  186. } else {
  187. dm = getDisplayMode();
  188. }
  189. if (fullScreenWindow != null && windowedModeBounds != null) {
  190. fullScreenWindow.setBounds(windowedModeBounds);
  191. }
  192. // Set the full screen window
  193. fullScreenWindow = w;
  194. if (fullScreenWindow != null) {
  195. windowedModeBounds = fullScreenWindow.getBounds();
  196. fullScreenWindow.setBounds(0, 0, dm.getWidth(), dm.getHeight());
  197. fullScreenWindow.setVisible(true);
  198. fullScreenWindow.toFront();
  199. }
  200. }
  201. /**
  202. * Returns the <code>Window</code> object representing the
  203. * full-screen window if the device is in full-screen mode.
  204. *
  205. * @return the full-screen window, or <code>null</code> if the device is
  206. * not in full-screen mode.
  207. * @see #setFullScreenWindow(Window)
  208. * @since 1.4
  209. */
  210. public Window getFullScreenWindow() {
  211. return fullScreenWindow;
  212. }
  213. /**
  214. * Returns <code>true</code> if this <code>GraphicsDevice</code>
  215. * supports low-level display changes.
  216. * @return whether low-level display changes are supported for this
  217. * graphics device. Note that this may or may not be dependent on
  218. * full-screen exclusive mode.
  219. * @see #setDisplayMode
  220. * @since 1.4
  221. */
  222. public boolean isDisplayChangeSupported() {
  223. return false;
  224. }
  225. /**
  226. * Sets the display mode of this graphics device. This may only be allowed
  227. * in full-screen, exclusive mode.
  228. * @param dm the new display mode of this graphics device
  229. * @exception IllegalArgumentException if the <code>DisplayMode</code>
  230. * supplied is <code>null</code>, or is not available in the array returned
  231. * by <code>getDisplayModes</code>
  232. * @exception UnsupportedOperationException if
  233. * <code>isDisplayChangeSupported</code> returns <code>false</code>
  234. * @see #getDisplayMode
  235. * @see #getDisplayModes
  236. * @see #isDisplayChangeSupported
  237. * @since 1.4
  238. */
  239. public void setDisplayMode(DisplayMode dm) {
  240. throw new UnsupportedOperationException("Cannot change display mode");
  241. }
  242. /**
  243. * Returns the current display mode of this
  244. * <code>GraphicsDevice</code>.
  245. * @return the current display mode of this graphics device.
  246. * @see #setDisplayMode(DisplayMode)
  247. * @since 1.4
  248. */
  249. public DisplayMode getDisplayMode() {
  250. GraphicsConfiguration gc = getDefaultConfiguration();
  251. Rectangle r = gc.getBounds();
  252. ColorModel cm = gc.getColorModel();
  253. return new DisplayMode(r.width, r.height, cm.getPixelSize(), 0);
  254. }
  255. /**
  256. * Returns all display modes available for this
  257. * <code>GraphicsDevice</code>.
  258. * @return all of the display modes available for this graphics device.
  259. * @since 1.4
  260. */
  261. public DisplayMode[] getDisplayModes() {
  262. return new DisplayMode[] { getDisplayMode() };
  263. }
  264. /**
  265. * This method returns the number of bytes available in
  266. * accelerated memory on this device.
  267. * Some images are created or cached
  268. * in accelerated memory on a first-come,
  269. * first-served basis. On some operating systems,
  270. * this memory is a finite resource. Calling this method
  271. * and scheduling the creation and flushing of images carefully may
  272. * enable applications to make the most efficient use of
  273. * that finite resource.
  274. * <br>
  275. * Note that the number returned is a snapshot of how much
  276. * memory is available; some images may still have problems
  277. * being allocated into that memory. For example, depending
  278. * on operating system, driver, memory configuration, and
  279. * thread situations, the full extent of the size reported
  280. * may not be available for a given image. There are further
  281. * inquiry methods on the {@link ImageCapabilities} object
  282. * associated with a VolatileImage that can be used to determine
  283. * whether a particular VolatileImage has been created in accelerated
  284. * memory.
  285. * @return number of bytes available in accelerated memory.
  286. * A negative return value indicates that accelerated memory
  287. * is unlimited.
  288. * @see java.awt.image.VolatileImage#flush
  289. * @see ImageCapabilities#isAccelerated
  290. */
  291. public int getAvailableAcceleratedMemory() {
  292. return -1;
  293. }
  294. }