1. /*
  2. * @(#)GraphicsConfiguration.java 1.35 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.geom.AffineTransform;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.ColorModel;
  11. import java.awt.image.VolatileImage;
  12. /**
  13. * The <code>GraphicsConfiguration</code> class describes the
  14. * characteristics of a graphics destination such as a printer or monitor.
  15. * There can be many <code>GraphicsConfiguration</code> objects associated
  16. * with a single graphics device, representing different drawing modes or
  17. * capabilities. The corresponding native structure will vary from platform
  18. * to platform. For example, on X11 windowing systems,
  19. * each visual is a different <code>GraphicsConfiguration</code>.
  20. * On Microsoft Windows, <code>GraphicsConfiguration</code>s represent
  21. * PixelFormats available in the current resolution and color depth.
  22. * <p>
  23. * In a virtual device multi-screen environment in which the desktop
  24. * area could span multiple physical screen devices, the bounds of the
  25. * <code>GraphicsConfiguration</code> objects are relative to the
  26. * virtual coordinate system. When setting the location of a
  27. * component, use {@link #getBounds() getBounds} to get the bounds of
  28. * the desired <code>GraphicsConfiguration</code> and offset the location
  29. * with the coordinates of the <code>GraphicsConfiguration</code>,
  30. * as the following code sample illustrates:
  31. * </p>
  32. *
  33. * <pre>
  34. * Frame f = new Frame(gc); // where gc is a GraphicsConfiguration
  35. * Rectangle bounds = gc.getBounds();
  36. * f.setLocation(10 + bounds.x, 10 + bounds.y); </pre>
  37. *
  38. * <p>
  39. * To determine if your environment is a virtual device
  40. * environment, call <code>getBounds</code> on all of the
  41. * <code>GraphicsConfiguration</code> objects in your system. If
  42. * any of the origins of the returned bounds is not (0, 0),
  43. * your environment is a virtual device environment.
  44. *
  45. * <p>
  46. * You can also use <code>getBounds</code> to determine the bounds
  47. * of the virtual device. To do this, first call <code>getBounds</code> on all
  48. * of the <code>GraphicsConfiguration</code> objects in your
  49. * system. Then calculate the union of all of the bounds returned
  50. * from the calls to <code>getBounds</code>. The union is the
  51. * bounds of the virtual device. The following code sample
  52. * calculates the bounds of the virtual device.
  53. *
  54. * <pre>
  55. * Rectangle virtualBounds = new Rectangle();
  56. * GraphicsEnvironment ge = GraphicsEnvironment.
  57. * getLocalGraphicsEnvironment();
  58. * GraphicsDevice[] gs =
  59. * ge.getScreenDevices();
  60. * for (int j = 0; j < gs.length; j++) {
  61. * GraphicsDevice gd = gs[j];
  62. * GraphicsConfiguration[] gc =
  63. * gd.getConfigurations();
  64. * for (int i=0; i < gc.length; i++) {
  65. * virtualBounds =
  66. * virtualBounds.union(gc[i].getBounds());
  67. * }
  68. * } </pre>
  69. *
  70. * @see Window
  71. * @see Frame
  72. * @see GraphicsEnvironment
  73. * @see GraphicsDevice
  74. */
  75. /*
  76. * REMIND: What to do about capabilities?
  77. * The
  78. * capabilities of the device can be determined by enumerating the possible
  79. * capabilities and checking if the GraphicsConfiguration
  80. * implements the interface for that capability.
  81. *
  82. * @version 1.35, 01/23/03
  83. */
  84. public abstract class GraphicsConfiguration {
  85. private static BufferCapabilities defaultBufferCaps;
  86. private static ImageCapabilities defaultImageCaps;
  87. /**
  88. * This is an abstract class that cannot be instantiated directly.
  89. * Instances must be obtained from a suitable factory or query method.
  90. *
  91. * @see GraphicsDevice#getConfigurations
  92. * @see GraphicsDevice#getDefaultConfiguration
  93. * @see GraphicsDevice#getBestConfiguration
  94. * @see Graphics2D#getDeviceConfiguration
  95. */
  96. protected GraphicsConfiguration() {
  97. }
  98. /**
  99. * Returns the {@link GraphicsDevice} associated with this
  100. * <code>GraphicsConfiguration</code>.
  101. * @return a <code>GraphicsDevice</code> object that is
  102. * associated with this <code>GraphicsConfiguration</code>.
  103. */
  104. public abstract GraphicsDevice getDevice();
  105. /**
  106. * Returns a {@link BufferedImage} with a data layout and color model
  107. * compatible with this <code>GraphicsConfiguration</code>. This
  108. * method has nothing to do with memory-mapping
  109. * a device. The returned <code>BufferedImage</code> has
  110. * a layout and color model that is closest to this native device
  111. * configuration and can therefore be optimally blitted to this
  112. * device.
  113. * @param width the width of the returned <code>BufferedImage</code>
  114. * @param height the height of the returned <code>BufferedImage</code>
  115. * @return a <code>BufferedImage</code> whose data layout and color
  116. * model is compatible with this <code>GraphicsConfiguration</code>.
  117. */
  118. public abstract BufferedImage createCompatibleImage(int width, int height);
  119. /**
  120. * Returns a {@link VolatileImage} with a data layout and color model
  121. * compatible with this <code>GraphicsConfiguration</code>.
  122. * The returned <code>VolatileImage</code>
  123. * may have data that is stored optimally for the underlying graphics
  124. * device and may therefore benefit from platform-specific rendering
  125. * acceleration.
  126. * @param width the width of the returned <code>VolatileImage</code>
  127. * @param height the height of the returned <code>VolatileImage</code>
  128. * @return a <code>VolatileImage</code> whose data layout and color
  129. * model is compatible with this <code>GraphicsConfiguration</code>.
  130. * @see Component#createVolatileImage(int, int)
  131. */
  132. public abstract VolatileImage createCompatibleVolatileImage(int width,
  133. int height);
  134. /**
  135. * Returns a {@link VolatileImage} with a data layout and color model
  136. * compatible with this <code>GraphicsConfiguration</code>, using
  137. * the specified image capabilities.
  138. * The returned <code>VolatileImage</code> has
  139. * a layout and color model that is closest to this native device
  140. * configuration and can therefore be optimally blitted to this
  141. * device.
  142. * @return a <code>VolatileImage</code> whose data layout and color
  143. * model is compatible with this <code>GraphicsConfiguration</code>.
  144. * @param width the width of the returned <code>VolatileImage</code>
  145. * @param height the height of the returned <code>VolatileImage</code>
  146. * @param caps the image capabilities
  147. * @exception AWTException if the supplied image capabilities could not
  148. * be met by this graphics configuration
  149. * @since 1.4
  150. */
  151. public VolatileImage createCompatibleVolatileImage(int width, int height,
  152. ImageCapabilities caps) throws AWTException {
  153. // REMIND : check caps
  154. return createCompatibleVolatileImage(width, height);
  155. }
  156. /**
  157. * Returns a <code>BufferedImage</code> that supports the specified
  158. * transparency and has a data layout and color model
  159. * compatible with this <code>GraphicsConfiguration</code>. This
  160. * method has nothing to do with memory-mapping
  161. * a device. The returned <code>BufferedImage</code> has a layout and
  162. * color model that can be optimally blitted to a device
  163. * with this <code>GraphicsConfiguration</code>.
  164. * @param width the width of the returned <code>BufferedImage</code>
  165. * @param height the height of the returned <code>BufferedImage</code>
  166. * @param transparency the specified transparency mode
  167. * @return a <code>BufferedImage</code> whose data layout and color
  168. * model is compatible with this <code>GraphicsConfiguration</code>
  169. * and also supports the specified transparency.
  170. * @see Transparency#OPAQUE
  171. * @see Transparency#BITMASK
  172. * @see Transparency#TRANSLUCENT
  173. */
  174. public abstract BufferedImage createCompatibleImage(int width, int height,
  175. int transparency);
  176. /**
  177. * Returns the {@link ColorModel} associated with this
  178. * <code>GraphicsConfiguration</code>.
  179. * @return a <code>ColorModel</code> object that is associated with
  180. * this <code>GraphicsConfiguration</code>.
  181. */
  182. public abstract ColorModel getColorModel();
  183. /**
  184. * Returns the <code>ColorModel</code> associated with this
  185. * <code>GraphicsConfiguration</code> that supports the specified
  186. * transparency.
  187. * @param transparency the specified transparency mode
  188. * @return a <code>ColorModel</code> object that is associated with
  189. * this <code>GraphicsConfiguration</code> and supports the
  190. * specified transparency.
  191. */
  192. public abstract ColorModel getColorModel(int transparency);
  193. /**
  194. * Returns the default {@link AffineTransform} for this
  195. * <code>GraphicsConfiguration</code>. This
  196. * <code>AffineTransform</code> is typically the Identity transform
  197. * for most normal screens. The default <code>AffineTransform</code>
  198. * maps coordinates onto the device such that 72 user space
  199. * coordinate units measure approximately 1 inch in device
  200. * space. The normalizing transform can be used to make
  201. * this mapping more exact. Coordinates in the coordinate space
  202. * defined by the default <code>AffineTransform</code> for screen and
  203. * printer devices have the origin in the upper left-hand corner of
  204. * the target region of the device, with X coordinates
  205. * increasing to the right and Y coordinates increasing downwards.
  206. * For image buffers not associated with a device, such as those not
  207. * created by <code>createCompatibleImage</code>,
  208. * this <code>AffineTransform</code> is the Identity transform.
  209. * @return the default <code>AffineTransform</code> for this
  210. * <code>GraphicsConfiguration</code>.
  211. */
  212. public abstract AffineTransform getDefaultTransform();
  213. /**
  214. *
  215. * Returns a <code>AffineTransform</code> that can be concatenated
  216. * with the default <code>AffineTransform</code>
  217. * of a <code>GraphicsConfiguration</code> so that 72 units in user
  218. * space equals 1 inch in device space.
  219. * <p>
  220. * For a particular {@link Graphics2D}, g, one
  221. * can reset the transformation to create
  222. * such a mapping by using the following pseudocode:
  223. * <pre>
  224. * GraphicsConfiguration gc = g.getGraphicsConfiguration();
  225. *
  226. * g.setTransform(gc.getDefaultTransform());
  227. * g.transform(gc.getNormalizingTransform());
  228. * </pre>
  229. * Note that sometimes this <code>AffineTransform</code> is identity,
  230. * such as for printers or metafile output, and that this
  231. * <code>AffineTransform</code> is only as accurate as the information
  232. * supplied by the underlying system. For image buffers not
  233. * associated with a device, such as those not created by
  234. * <code>createCompatibleImage</code>, this
  235. * <code>AffineTransform</code> is the Identity transform
  236. * since there is no valid distance measurement.
  237. * @return an <code>AffineTransform</code> to concatenate to the
  238. * default <code>AffineTransform</code> so that 72 units in user
  239. * space is mapped to 1 inch in device space.
  240. */
  241. public abstract AffineTransform getNormalizingTransform();
  242. /**
  243. * Returns the bounds of the <code>GraphicsConfiguration</code>
  244. * in the device coordinates. In a multi-screen environment
  245. * with a virtual device, the bounds can have negative X
  246. * or Y origins.
  247. * @return the bounds of the area covered by this
  248. * <code>GraphicsConfiguration</code>.
  249. * @since 1.3
  250. */
  251. public abstract Rectangle getBounds();
  252. private static class DefaultBufferCapabilities extends BufferCapabilities {
  253. public DefaultBufferCapabilities(ImageCapabilities imageCaps) {
  254. super(imageCaps, imageCaps, null);
  255. }
  256. }
  257. /**
  258. * Returns the buffering capabilities of this
  259. * <code>GraphicsConfiguration</code>.
  260. * @return the buffering capabilities of this graphics
  261. * configuration object
  262. * @since 1.4
  263. */
  264. public BufferCapabilities getBufferCapabilities() {
  265. if (defaultBufferCaps == null) {
  266. defaultBufferCaps = new DefaultBufferCapabilities(
  267. getImageCapabilities());
  268. }
  269. return defaultBufferCaps;
  270. }
  271. /**
  272. * Returns the image capabilities of this
  273. * <code>GraphicsConfiguration</code>.
  274. * @return the image capabilities of this graphics
  275. * configuration object
  276. * @since 1.4
  277. */
  278. public ImageCapabilities getImageCapabilities() {
  279. if (defaultImageCaps == null) {
  280. defaultImageCaps = new ImageCapabilities(false);
  281. }
  282. return defaultImageCaps;
  283. }
  284. }