Results 1 to 6 of 6
  1. #1
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default Problem with loading image

    My problem is as follows, I load an image with ImageIO.read(File file) method, then I draw the image into a JPanel, contained in a JScrollPane. The problem is, the image appears only after I first scroll the JScrollPane, and the sliders only get the right size for the image after I resize the JFrame, but then, the image goes gray, until I scroll around. Here's the aproximate way I did it:
    Java Code:
    public class ImageViewer extends JFrame {
      JPanel panel;
      Image img;
      JScrollPane scroll;
      
      public ImageViewer() {
        panel = new JPanel() {
          public void paintComponent(Graphics g) {
            super.paintComponent(g);
            showImage();
          }
        }
        img = null;
        scroll = new JScrollPane();
        scroll.setViewportView(panel);
      }  
    
      private void performOpen() { //used in menu
        //get the image from a file using JFileChooser, the image is saved in img
      }  
    
      private void showImage() {
        if(img != null) {
          panel.setPreferredSize(new Dimension(img.getWidth(null), img.getHeight(null));
          Graphics g = panel.getGraphics();
          g.drawImage(img, 0, 0, null);
          panel.repaint();
        }
      }
    }
    I also tried adding a ComponentListener that called panel.repaint(); on resizing, but the image still goes gray if I resize the window, and shows up only after I scroll.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Shouldn't that panel (re)draw that image in its paintComponent( ... ) method, everytime it's called by the EDT?

    kind regards,

    Jos

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Thanks, I went through the tutorial and I corrected my errors, so the image displays fine.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    My take on your code (without my compiler handy)
    Java Code:
      public ImageViewer() {
        panel = new JPanel() {
          public void paintComponent(Graphics g) {
            super.paintComponent(g);
            showImage(g); // **** 
          }
        }
        img = null;
        scroll = new JScrollPane();
        scroll.setViewportView(panel);
      }

    and
    Java Code:
      private void showImage(Graphics g) {
        if(img != null) {
          // **** I wouldn't set the panel's pref size here but rather when the image is read or passed into the panel
          // ****panel.setPreferredSize(new Dimension(img.getWidth(null), img.getHeight(null));
          // ****Graphics g = panel.getGraphics();
          g.drawImage(img, 0, 0, null);
          // **** ???? panel.repaint();
        }
      }
    edit: nevermind.... :)

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by curmudgeon View Post
    Java Code:
      private void showImage(Graphics g) {
        if(img != null) {
          // **** I wouldn't set the panel's pref size here but rather when the image is read or passed into the panel
          // ****panel.setPreferredSize(new Dimension(img.getWidth(null), img.getHeight(null));
          // ****Graphics g = panel.getGraphics();
          g.drawImage(img, 0, 0, null);
          // **** ???? panel.repaint();
        }
      }
    After I went through the tutorial I saw, that I should pass the Graphics object from the paintComponent method to my showImage method. About the preferred size of the JPanel, img gets initialized in the performOpen method, so I set its size only after I already have an Image object to work on. The repaint() call no longer features in the code. Now, the only remaining problem I'm having, is that the JScrollPane doesn't update it's sliders after the image is loaded and displayed, but after I resize the JFrame, it works correctly. I've been going through JScrollPanes API, but haven't found a method that does what I need yet. Any help on that?
    EDIT: found the revalidate() method. Thanks for the help guys!
    Last edited by m00nchile; 05-25-2010 at 03:08 PM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Dynamic image loading in jsp
    By ramakrishna k m in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 10-10-2011, 07:08 AM
  2. Problem in loading Image
    By rahulm87 in forum Java 2D
    Replies: 1
    Last Post: 09-08-2009, 05:45 PM
  3. Loading image in applet
    By syarizma in forum Advanced Java
    Replies: 0
    Last Post: 08-06-2009, 10:02 AM
  4. loading image into applet
    By balaram in forum Java Applets
    Replies: 1
    Last Post: 11-06-2008, 03:13 PM
  5. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 09:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •