Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4653370

prepareImage ignores width and height parameters



    • Subcomponent:
    • CPU:
    • OS:


      The specification for prepareImage states that, if the width and height parameters are not both -1, then the image is prepared and scaled to the supplied width. The book "The Java Class Libraries - An Annotated Reference" by Patrick Chan and Rosanna Lee also states that:

      "This method is typically used to preload image data for an image or a scaled version of an image so that Graphics.drawImage() can operate as quickly as possible."

      However, Java ignores the width and height parameters passed to prepareImage and always prepares as though -1, -1 had been specified - i.e. at the origional image size.

      The test below demonstrates this problem - you will notice that the failed line is pronted and the image is not scaled to 300,300 when it is drawn:

      (to run the test pass an image file as the first argument to this program)

      import java.awt.image.*;
      import java.awt.*;

      public class PrepareTest extends Frame
              private Image image;

              public PrepareTest (Image image)
                      this.image = image;

              public void paint (Graphics g)

              public static void main(String[] args) throws Exception
                      Toolkit tk = Toolkit.getDefaultToolkit();
                      Image image = tk.getImage(args[0]);
                      final Object signal = new Object();
                      ImageObserver obs = new ImageObserver()
                              public boolean imageUpdate(Image img, int infoflags,
                                      int x, int y, int width, int height)
                                      System.out.println("update: " + infoflags + " x = " + x + " y = " + y + " width = " + width + " height = " + height);

                                      if ((infoflags & ALLBITS) != 0)

                                      return true;

      // prepareImage at scaled size of 300,300

                      tk.prepareImage(image, 300, 300, obs);

      // wait for image to complete loading


      // Check width and height are the correct scaled size

                      int width = image.getWidth(obs);
      int height = image.getHeight(obs);

      if (width != 300 || height != 300)
      System.out.println("Failed: Size is not 300,300: width = " + width + ", height = " + height);

      else System.out.println("Test passed!");

      // Display image to make sure it is scaled

                      Frame f = new PrepareTest(image);

      We are currenly working on the implementation of the J2ME basis platform and would definately like for the intended behaviour of prepareImage to be present. This is because we can store the image at a smaller size and also speeds drawing of the image (the image won't have to be scaled on the fly). This would save memory for us which is of vital importance in an embedded device. However, basis application are supposed to run on J2SE too so this would be a cause of incompatiability. We have currently implemented the spec behaviour but would like this to be fixed in J2SE as soon as possible.

      A similar bug was reported in 1.0.1 but it is marked as fixed and integrated. Clearly it has not been or has reemerged again!




            flar Jim Graham
            duke J. Duke (Inactive)
            0 Vote for this issue
            0 Start watching this issue