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