1. /*
  2. * @(#)GraphicsConfiguration.java 1.38 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.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.38, 12/19/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>.
  137. * The returned <code>VolatileImage</code>
  138. * may have data that is stored optimally for the underlying graphics
  139. * device and may therefore benefit from platform-specific rendering
  140. * acceleration.
  141. * @param width the width of the returned <code>VolatileImage</code>
  142. * @param height the height of the returned <code>VolatileImage</code>
  143. * @param transparency the specified transparency mode
  144. * @return a <code>VolatileImage</code> whose data layout and color
  145. * model is compatible with this <code>GraphicsConfiguration</code>.
  146. * @throws IllegalArgumentException if the transparency is not a valid value
  147. * @see Transparency#OPAQUE
  148. * @see Transparency#BITMASK
  149. * @see Transparency#TRANSLUCENT
  150. * @see Component#createVolatileImage(int, int)
  151. * @since 1.5
  152. */
  153. public abstract VolatileImage
  154. createCompatibleVolatileImage(int width, int height, int transparency);
  155. /**
  156. * Returns a {@link VolatileImage} with a data layout and color model
  157. * compatible with this <code>GraphicsConfiguration</code>, using
  158. * the specified image capabilities.
  159. * The returned <code>VolatileImage</code> has
  160. * a layout and color model that is closest to this native device
  161. * configuration and can therefore be optimally blitted to this
  162. * device.
  163. * @return a <code>VolatileImage</code> whose data layout and color
  164. * model is compatible with this <code>GraphicsConfiguration</code>.
  165. * @param width the width of the returned <code>VolatileImage</code>
  166. * @param height the height of the returned <code>VolatileImage</code>
  167. * @param caps the image capabilities
  168. * @exception AWTException if the supplied image capabilities could not
  169. * be met by this graphics configuration
  170. * @since 1.4
  171. */
  172. public VolatileImage createCompatibleVolatileImage(int width, int height,
  173. ImageCapabilities caps) throws AWTException {
  174. // REMIND : check caps
  175. return createCompatibleVolatileImage(width, height);
  176. }
  177. /**
  178. * Returns a {@link VolatileImage} with a data layout and color model
  179. * compatible with this <code>GraphicsConfiguration</code>, using
  180. * the specified image capabilities and transparency value.
  181. * The returned <code>VolatileImage</code> has
  182. * a layout and color model that is closest to this native device
  183. * configuration and can therefore be optimally blitted to this
  184. * device.
  185. * @param width the width of the returned <code>VolatileImage</code>
  186. * @param height the height of the returned <code>VolatileImage</code>
  187. * @param caps the image capabilities
  188. * @param transparency the specified transparency mode
  189. * @return a <code>VolatileImage</code> whose data layout and color
  190. * model is compatible with this <code>GraphicsConfiguration</code>.
  191. * @see Transparency#OPAQUE
  192. * @see Transparency#BITMASK
  193. * @see Transparency#TRANSLUCENT
  194. * @throws IllegalArgumentException if the transparency is not a valid value
  195. * @exception AWTException if the supplied image capabilities could not
  196. * be met by this graphics configuration
  197. * @see Component#createVolatileImage(int, int)
  198. * @since 1.5
  199. */
  200. public VolatileImage createCompatibleVolatileImage(int width, int height,
  201. ImageCapabilities caps, int transparency) throws AWTException
  202. {
  203. // REMIND : check caps
  204. return createCompatibleVolatileImage(width, height, transparency);
  205. }
  206. /**
  207. * Returns a <code>BufferedImage</code> that supports the specified
  208. * transparency and has a data layout and color model
  209. * compatible with this <code>GraphicsConfiguration</code>. This
  210. * method has nothing to do with memory-mapping
  211. * a device. The returned <code>BufferedImage</code> has a layout and
  212. * color model that can be optimally blitted to a device
  213. * with this <code>GraphicsConfiguration</code>.
  214. * @param width the width of the returned <code>BufferedImage</code>
  215. * @param height the height of the returned <code>BufferedImage</code>
  216. * @param transparency the specified transparency mode
  217. * @return a <code>BufferedImage</code> whose data layout and color
  218. * model is compatible with this <code>GraphicsConfiguration</code>
  219. * and also supports the specified transparency.
  220. * @throws IllegalArgumentException if the transparency is not a valid value
  221. * @see Transparency#OPAQUE
  222. * @see Transparency#BITMASK
  223. * @see Transparency#TRANSLUCENT
  224. */
  225. public abstract BufferedImage createCompatibleImage(int width, int height,
  226. int transparency);
  227. /**
  228. * Returns the {@link ColorModel} associated with this
  229. * <code>GraphicsConfiguration</code>.
  230. * @return a <code>ColorModel</code> object that is associated with
  231. * this <code>GraphicsConfiguration</code>.
  232. */
  233. public abstract ColorModel getColorModel();
  234. /**
  235. * Returns the <code>ColorModel</code> associated with this
  236. * <code>GraphicsConfiguration</code> that supports the specified
  237. * transparency.
  238. * @param transparency the specified transparency mode
  239. * @return a <code>ColorModel</code> object that is associated with
  240. * this <code>GraphicsConfiguration</code> and supports the
  241. * specified transparency or null if the transparency is not a valid
  242. * value.
  243. * @see Transparency#OPAQUE
  244. * @see Transparency#BITMASK
  245. * @see Transparency#TRANSLUCENT
  246. */
  247. public abstract ColorModel getColorModel(int transparency);
  248. /**
  249. * Returns the default {@link AffineTransform} for this
  250. * <code>GraphicsConfiguration</code>. This
  251. * <code>AffineTransform</code> is typically the Identity transform
  252. * for most normal screens. The default <code>AffineTransform</code>
  253. * maps coordinates onto the device such that 72 user space
  254. * coordinate units measure approximately 1 inch in device
  255. * space. The normalizing transform can be used to make
  256. * this mapping more exact. Coordinates in the coordinate space
  257. * defined by the default <code>AffineTransform</code> for screen and
  258. * printer devices have the origin in the upper left-hand corner of
  259. * the target region of the device, with X coordinates
  260. * increasing to the right and Y coordinates increasing downwards.
  261. * For image buffers not associated with a device, such as those not
  262. * created by <code>createCompatibleImage</code>,
  263. * this <code>AffineTransform</code> is the Identity transform.
  264. * @return the default <code>AffineTransform</code> for this
  265. * <code>GraphicsConfiguration</code>.
  266. */
  267. public abstract AffineTransform getDefaultTransform();
  268. /**
  269. *
  270. * Returns a <code>AffineTransform</code> that can be concatenated
  271. * with the default <code>AffineTransform</code>
  272. * of a <code>GraphicsConfiguration</code> so that 72 units in user
  273. * space equals 1 inch in device space.
  274. * <p>
  275. * For a particular {@link Graphics2D}, g, one
  276. * can reset the transformation to create
  277. * such a mapping by using the following pseudocode:
  278. * <pre>
  279. * GraphicsConfiguration gc = g.getGraphicsConfiguration();
  280. *
  281. * g.setTransform(gc.getDefaultTransform());
  282. * g.transform(gc.getNormalizingTransform());
  283. * </pre>
  284. * Note that sometimes this <code>AffineTransform</code> is identity,
  285. * such as for printers or metafile output, and that this
  286. * <code>AffineTransform</code> is only as accurate as the information
  287. * supplied by the underlying system. For image buffers not
  288. * associated with a device, such as those not created by
  289. * <code>createCompatibleImage</code>, this
  290. * <code>AffineTransform</code> is the Identity transform
  291. * since there is no valid distance measurement.
  292. * @return an <code>AffineTransform</code> to concatenate to the
  293. * default <code>AffineTransform</code> so that 72 units in user
  294. * space is mapped to 1 inch in device space.
  295. */
  296. public abstract AffineTransform getNormalizingTransform();
  297. /**
  298. * Returns the bounds of the <code>GraphicsConfiguration</code>
  299. * in the device coordinates. In a multi-screen environment
  300. * with a virtual device, the bounds can have negative X
  301. * or Y origins.
  302. * @return the bounds of the area covered by this
  303. * <code>GraphicsConfiguration</code>.
  304. * @since 1.3
  305. */
  306. public abstract Rectangle getBounds();
  307. private static class DefaultBufferCapabilities extends BufferCapabilities {
  308. public DefaultBufferCapabilities(ImageCapabilities imageCaps) {
  309. super(imageCaps, imageCaps, null);
  310. }
  311. }
  312. /**
  313. * Returns the buffering capabilities of this
  314. * <code>GraphicsConfiguration</code>.
  315. * @return the buffering capabilities of this graphics
  316. * configuration object
  317. * @since 1.4
  318. */
  319. public BufferCapabilities getBufferCapabilities() {
  320. if (defaultBufferCaps == null) {
  321. defaultBufferCaps = new DefaultBufferCapabilities(
  322. getImageCapabilities());
  323. }
  324. return defaultBufferCaps;
  325. }
  326. /**
  327. * Returns the image capabilities of this
  328. * <code>GraphicsConfiguration</code>.
  329. * @return the image capabilities of this graphics
  330. * configuration object
  331. * @since 1.4
  332. */
  333. public ImageCapabilities getImageCapabilities() {
  334. if (defaultImageCaps == null) {
  335. defaultImageCaps = new ImageCapabilities(false);
  336. }
  337. return defaultImageCaps;
  338. }
  339. }