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

[macosx] MultiResolutionImage: toolkit.getImage('fileName') output class depends on used file name

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • 9
    • 9
    • client-libs

      Probably not a bug, but: should we use the first (1x) file name _only_ to get the desired MultiResolutionImage? (is it specified somewhere?)

      Please run the following test example:

      import java.awt.*;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import javax.imageio.ImageIO;
      import sun.awt.image.MultiResolutionImage;

      public class Test {

          private static final int IMAGE_WIDTH = 300;
          private static final int IMAGE_HEIGHT = 200;
          private static final Color COLOR_1X = Color.GREEN;
          private static final Color COLOR_2X = Color.BLUE;
          private static final String IMAGE_NAME_1X = "image.png";
          private static final String IMAGE_NAME_2X = "image@2x.png";

          public static void main(String[] args) throws Exception {

              generateImages();

              File imageFile = new File(IMAGE_NAME_1X); // ?
              String fileName = imageFile.getAbsolutePath();

              Image img = Toolkit.getDefaultToolkit().getImage(fileName);
              System.out.println("file: " + fileName);

              System.out.println("image class: " + img.getClass().getCanonicalName());
              MultiResolutionImage mri = (MultiResolutionImage) img;
          }

          static void generateImages() throws Exception {
              if (!new File(IMAGE_NAME_1X).exists()) { generateImage(1); }
              if (!new File(IMAGE_NAME_2X).exists()) { generateImage(2); }
          }

          static void generateImage(int scale) throws Exception {

              BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
              Graphics g = image.getGraphics();
              g.setColor(scale == 1 ? COLOR_1X : COLOR_2X);
              g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);
              File file = new File(scale == 1 ? IMAGE_NAME_1X : IMAGE_NAME_2X);
              ImageIO.write(image, "png", file);
          }
      }

      Output (Mac OS X 10.10.3, JDK9 b77) is as expected:
        file: /Users/xxx/work/tests/Test1/image.png
        image class: sun.awt.image.MultiResolutionToolkitImage

      Then please replace "IMAGE_NAME_1X" with "IMAGE_NAME_2X" in line "File imageFile = ..."; the output will be:
        file: /Users/xxx/work/tests/Test1/image@2x.png
        image class: sun.awt.image.ToolkitImage
         Exception in thread "main" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to sun.awt.image.MultiResolutionImage
              at Test.main(Test.java:27)

      is it an expected result?

            alexsch Alexandr Scherbatiy
            avstepan Alexander Stepanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: