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

Large images are not rendered on JPanel using Jlabel or directly using Graphics

XMLWordPrintable

    • 2d
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Linux - Ubuntu 22.04.1 with Wayland dm
      Environment Variables:
      DISPLAY :0
      MACHTYPE x86_64-pc-linux-gnu


      A DESCRIPTION OF THE PROBLEM :
      When creating an image with a large dimension (width and/or height >= 32768), the image is no longer rendered in the JPanel on the window. This seems to affect at least java versions 8, 11, and 17. Also, overriding the paint method of the JPanel using drawImage produces the same result. It only renders with both dimensions < 32768. Exporting the BufferedImage to the filesystem produces expected results, so it seems the issue is at the rendering stage.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a JFrame with a JPanel contained by a JScrollpane.
      2. Create a random image of either width or height >= 32768 pixels
      3. Add this image to a JLabel and to the JPanel

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Fully rendered image on the window
      ACTUAL -
      Blank background of the window

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.HeadlessException;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      import java.util.Random;

      import javax.imageio.ImageIO;
      import javax.swing.BoxLayout;
      import javax.swing.ImageIcon;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;

      public class LargeImageTest extends JFrame {

      private final Random random = new Random();

      /**
      * Constructor.
      * @throws HeadlessException
      */
      public LargeImageTest() throws HeadlessException {
      super("Large Image Test");
      setDefaultCloseOperation(EXIT_ON_CLOSE);

      createGuiWithPanelLabel();
      }

      private void createGuiWithPanelLabel() {
      // create panel
      final JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

      // create random image of given size
      // final BufferedImage image = createRandomImage(800, 32767);
      final BufferedImage image = createRandomImage(800, 32768);
      panel.add(new JLabel(new ImageIcon(image)));

      // add panel to scrollpane
      final JScrollPane scrollPane = new JScrollPane(panel);
      scrollPane.setPreferredSize(new Dimension(image.getWidth() + 20, 600));
      getContentPane().add(scrollPane);

      // open window
      pack();
      setVisible(true);
      }

      private void createGuiWithPanelGraphics() {
      // create random image of given size
      // final BufferedImage image = createRandomImage(800, 32767);
      final BufferedImage image = createRandomImage(800, 32768);

      // create panel
      final JPanel panel = new JPanel() {
      @Override
      public void paint(final Graphics g) {
      g.drawImage(image, 0, 0, LargeImageTest.this);
      }
      };

      panel.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

      // add panel to scrollpane
      final JScrollPane scrollPane = new JScrollPane(panel);
      scrollPane.setPreferredSize(new Dimension(image.getWidth() + 20, 600));
      getContentPane().add(scrollPane);

      // open window
      pack();
      setVisible(true);
      }

      private BufferedImage createRandomImage(final int width, final int height) {
      final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      final Graphics2D g = image.createGraphics();
      for (int r = 0; r < height; r++) {
      for (int c = 0; c < width; c++) {
      g.setColor(new Color(random.nextInt()));
      g.fillRect(c, r, 1, 1);
      }
      }
      return image;
      }

      private void exportImage(final BufferedImage image) {
      try {
      ImageIO.write(image, "jpg", new File("out.jpg"));
      }
      catch (final IOException e) {
      e.printStackTrace();
      }
      }

      /**
      * @param args
      */
      public static void main(final String[] args) {
      new LargeImageTest();
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            pnarayanaswa Praveen Narayanaswamy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: