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

getBounds call on graphics.getDeviceConfiguration() returning cached information

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • 6u16, 7, 7u45, 8, 8u31, 9
    • client-libs
    • 2d
    • b89
    • x86
    • windows_7

        FULL PRODUCT VERSION :
        java version "1.7.0_45", java version "1.6.0_06". Since it is a simple bug I am sure it is there in java 8 also.

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        When I call graphics.getDeviceConfiguration().getBounds() on a Graphics2D object got on a BufferedImage read from a file I continue to get the previous image dimensions after reading a new image.

        Please see details with sample program in steps to reproduce.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Below is a sample program which prints image bounds of jpeg files passed as command line args.

        Try calling it with paths to 2 images of different dimensions (Example: java GraphicsBug image1.jpg image2.jpg. The dimension of the first image is printed the second time also.

        import java.awt.Graphics2D;
        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.IOException;

        import javax.imageio.ImageIO;
        import javax.imageio.ImageReader;
        import javax.imageio.stream.ImageInputStream;


        public class GraphicsBug {

            public static void loadJpeg(String imagePath) throws IOException {
                ImageReader reader;
                File srcImageFile = new File(imagePath);
                ImageInputStream iis = ImageIO.createImageInputStream(srcImageFile);
                reader = (ImageReader) ImageIO.getImageReaders(iis).next();
                reader.setInput(iis);
                BufferedImage image = reader.read(0);
                Graphics2D graphics = image.createGraphics();
                System.out.println("Image Bounds from Graphics = " + graphics.getDeviceConfiguration().getBounds());
            }

            public static void main(String [] args) throws IOException {
                for (int i = 0; i < args.length; i++) {
                    loadJpeg(args[i]);
                }
            }
        }


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The correct image dimensions should be printed for each image. Below are two runs with Expected results:

        java GraphicsBug image1.jpg image2.jpg

        Image Bounds from Graphics = java.awt.Rectangle[x=0,y=0,width=496,height=64]
        Image Bounds from Graphics = java.awt.Rectangle[x=0,y=0,width=672,height=80]

        java GraphicsBug image2.jpg
        Image Bounds from Graphics = java.awt.Rectangle[x=0,y=0,width=672,height=80]
        ACTUAL -
        In the first run with 2 images the dimensions of the 1st image is duplicated:

        java GraphicsBug image1.jpg image2.jpg
        Image Bounds from Graphics = java.awt.Rectangle[x=0,y=0,width=496,height=64]
        Image Bounds from Graphics = java.awt.Rectangle[x=0,y=0,width=496,height=64]


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.awt.Graphics2D;
        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.IOException;

        import javax.imageio.ImageIO;
        import javax.imageio.ImageReader;
        import javax.imageio.stream.ImageInputStream;


        public class GraphicsBug {

            public static void loadJpeg(String imagePath) throws IOException {
                ImageReader reader;
                File srcImageFile = new File(imagePath);
                ImageInputStream iis = ImageIO.createImageInputStream(srcImageFile);
                reader = (ImageReader) ImageIO.getImageReaders(iis).next();
                reader.setInput(iis);
                BufferedImage image = reader.read(0);
                Graphics2D graphics = image.createGraphics();
                System.out.println("Image Bounds from Graphics = " + graphics.getDeviceConfiguration().getBounds());
            }

            // Pass Paths to 2 or more images with different dimensions and see the bound printed
            public static void main(String [] args) throws IOException {
                for (int i = 0; i < args.length; i++) {
                    loadJpeg(args[i]);
                }
            }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Do not use graphics.getDeviceConfiguration to get the image bounds get it directly from image.getWidth() and image.getHeight()

              psadhukhan Prasanta Sadhukhan
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

                Created:
                Updated:
                Resolved: