1. /*
  2. * @(#)Image.java 1.33 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.ImageProducer;
  9. import java.awt.image.ImageObserver;
  10. import java.awt.image.ImageFilter;
  11. import java.awt.image.FilteredImageSource;
  12. import java.awt.image.AreaAveragingScaleFilter;
  13. import java.awt.image.ReplicateScaleFilter;
  14. /**
  15. * The abstract class <code>Image</code> is the superclass of all
  16. * classes that represent graphical images. The image must be
  17. * obtained in a platform-specific manner.
  18. *
  19. * @version 1.33, 01/23/03
  20. * @author Sami Shaio
  21. * @author Arthur van Hoff
  22. * @since JDK1.0
  23. */
  24. public abstract class Image {
  25. /**
  26. * Determines the width of the image. If the width is not yet known,
  27. * this method returns <code>-1</code> and the specified
  28. * <code>ImageObserver</code> object is notified later.
  29. * @param observer an object waiting for the image to be loaded.
  30. * @return the width of this image, or <code>-1</code>
  31. * if the width is not yet known.
  32. * @see java.awt.Image#getHeight
  33. * @see java.awt.image.ImageObserver
  34. */
  35. public abstract int getWidth(ImageObserver observer);
  36. /**
  37. * Determines the height of the image. If the height is not yet known,
  38. * this method returns <code>-1</code> and the specified
  39. * <code>ImageObserver</code> object is notified later.
  40. * @param observer an object waiting for the image to be loaded.
  41. * @return the height of this image, or <code>-1</code>
  42. * if the height is not yet known.
  43. * @see java.awt.Image#getWidth
  44. * @see java.awt.image.ImageObserver
  45. */
  46. public abstract int getHeight(ImageObserver observer);
  47. /**
  48. * Gets the object that produces the pixels for the image.
  49. * This method is called by the image filtering classes and by
  50. * methods that perform image conversion and scaling.
  51. * @return the image producer that produces the pixels
  52. * for this image.
  53. * @see java.awt.image.ImageProducer
  54. */
  55. public abstract ImageProducer getSource();
  56. /**
  57. * Creates a graphics context for drawing to an off-screen image.
  58. * This method can only be called for off-screen images.
  59. * @return a graphics context to draw to the off-screen image.
  60. * @see java.awt.Graphics
  61. * @see java.awt.Component#createImage(int, int)
  62. */
  63. public abstract Graphics getGraphics();
  64. /**
  65. * Gets a property of this image by name.
  66. * <p>
  67. * Individual property names are defined by the various image
  68. * formats. If a property is not defined for a particular image, this
  69. * method returns the <code>UndefinedProperty</code> object.
  70. * <p>
  71. * If the properties for this image are not yet known, this method
  72. * returns <code>null</code>, and the <code>ImageObserver</code>
  73. * object is notified later.
  74. * <p>
  75. * The property name <code>"comment"</code> should be used to store
  76. * an optional comment which can be presented to the application as a
  77. * description of the image, its source, or its author.
  78. * @param name a property name.
  79. * @param observer an object waiting for this image to be loaded.
  80. * @return the value of the named property.
  81. * @see java.awt.image.ImageObserver
  82. * @see java.awt.Image#UndefinedProperty
  83. */
  84. public abstract Object getProperty(String name, ImageObserver observer);
  85. /**
  86. * The <code>UndefinedProperty</code> object should be returned whenever a
  87. * property which was not defined for a particular image is fetched.
  88. */
  89. public static final Object UndefinedProperty = new Object();
  90. /**
  91. * Creates a scaled version of this image.
  92. * A new <code>Image</code> object is returned which will render
  93. * the image at the specified <code>width</code> and
  94. * <code>height</code> by default. The new <code>Image</code> object
  95. * may be loaded asynchronously even if the original source image
  96. * has already been loaded completely. If either the <code>width</code>
  97. * or <code>height</code> is a negative number then a value is
  98. * substituted to maintain the aspect ratio of the original image
  99. * dimensions.
  100. * @param width the width to which to scale the image.
  101. * @param height the height to which to scale the image.
  102. * @param hints flags to indicate the type of algorithm to use
  103. * for image resampling.
  104. * @return a scaled version of the image.
  105. * @see java.awt.Image#SCALE_DEFAULT
  106. * @see java.awt.Image#SCALE_FAST
  107. * @see java.awt.Image#SCALE_SMOOTH
  108. * @see java.awt.Image#SCALE_REPLICATE
  109. * @see java.awt.Image#SCALE_AREA_AVERAGING
  110. * @since JDK1.1
  111. */
  112. public Image getScaledInstance(int width, int height, int hints) {
  113. ImageFilter filter;
  114. if ((hints & (SCALE_SMOOTH | SCALE_AREA_AVERAGING)) != 0) {
  115. filter = new AreaAveragingScaleFilter(width, height);
  116. } else {
  117. filter = new ReplicateScaleFilter(width, height);
  118. }
  119. ImageProducer prod;
  120. prod = new FilteredImageSource(getSource(), filter);
  121. return Toolkit.getDefaultToolkit().createImage(prod);
  122. }
  123. /**
  124. * Use the default image-scaling algorithm.
  125. * @since JDK1.1
  126. */
  127. public static final int SCALE_DEFAULT = 1;
  128. /**
  129. * Choose an image-scaling algorithm that gives higher priority
  130. * to scaling speed than smoothness of the scaled image.
  131. * @since JDK1.1
  132. */
  133. public static final int SCALE_FAST = 2;
  134. /**
  135. * Choose an image-scaling algorithm that gives higher priority
  136. * to image smoothness than scaling speed.
  137. * @since JDK1.1
  138. */
  139. public static final int SCALE_SMOOTH = 4;
  140. /**
  141. * Use the image scaling algorithm embodied in the
  142. * <code>ReplicateScaleFilter</code> class.
  143. * The <code>Image</code> object is free to substitute a different filter
  144. * that performs the same algorithm yet integrates more efficiently
  145. * into the imaging infrastructure supplied by the toolkit.
  146. * @see java.awt.image.ReplicateScaleFilter
  147. * @since JDK1.1
  148. */
  149. public static final int SCALE_REPLICATE = 8;
  150. /**
  151. * Use the Area Averaging image scaling algorithm. The
  152. * image object is free to substitute a different filter that
  153. * performs the same algorithm yet integrates more efficiently
  154. * into the image infrastructure supplied by the toolkit.
  155. * @see java.awt.image.AreaAveragingScaleFilter
  156. * @since JDK1.1
  157. */
  158. public static final int SCALE_AREA_AVERAGING = 16;
  159. /**
  160. * Flushes all resources being used by this Image object. This
  161. * includes any pixel data that is being cached for rendering to
  162. * the screen as well as any system resources that are being used
  163. * to store data or pixels for the image. The image is reset to
  164. * a state similar to when it was first created so that if it is
  165. * again rendered, the image data will have to be recreated or
  166. * fetched again from its source.
  167. * <p>
  168. * This method always leaves the image in a state such that it can
  169. * be reconstructed. This means the method applies only to cached
  170. * or other secondary representations of images such as those that
  171. * have been generated from an <tt>ImageProducer</tt> (read from a
  172. * file, for example). It does nothing for off-screen images that
  173. * have only one copy of their data.
  174. */
  175. public abstract void flush();
  176. }