Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Repainting an image

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Repainting an image

    I have this class. Upon initialization, I see the image I want, but when I call load() a second or third time, the image doesn't refresh. How can I repaint the image?

    public class ImgPanel extends JPanel {
      JLabel imgLbl;
     
      public ImgPanel(String initPath) {
        load( initPath);
        add( imgLbl );
      }
     
      public void load(String path) {
        BufferedImage img = ImageIO.read( new File( path ) );
        imgLbl = new JLabel(new ImageIcon( img ) );
    //  repaint();  <-- no effect
    //  imgLbl.repaint(); <-- no effect
      }
    }

    I'm also trying this way (same issue)
    public class ImgPanel extends JPanel{
      BufferedImage img;
     
      public ImgPanel(String initPath) {
        load( initPath );
      }
     
      public void load(String path) {
        img = ImageIO.read(new File(path) );
        repaint();
      }
     
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, null);
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Repainting an image

    The JLabel you added to the container is not being changed. Save the reference to the JLabel that was added and call its methods to change the image it is showing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Repainting an image

    Comming from a C/C++ world, this is the hardest thing for me about Java. I haven't figured out what is a reference and what is a copy. Could you write a line or two of code to explain that?

    I've tried to re-add the object, but that hasn't helped.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Repainting an image

    When the add(var) method is called, the value in var is copied into the container's variables. Future changes to the contents of var will not change the value that was copied when add() was called.
    In this example, save the contents of var and use it to access the object that you want to change.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Repainting an image

    Got it, thanks.

    This is what it looks like now:
    public class ImgPanel extends JPanel {
      JLabel imgLbl;
     
      public ImgPanel(String initPath) {
        imgLbl = new JLabel(new ImageIcon());
        add( imgLbl );
        load( initPath );
      }
     
      public void load(String path) {
        BufferedImage img = ImageIO.read( new File( path ) );
        imgLbl.setIcon(new ImageIcon( img ) );
      }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Repainting an image

    That looks like it could work.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Repainting an image

    This looks like an easy code to remember. Where exactly do you think you might send this information to? I was thinking of sending one like this to employer with information. It would be a neat look on their computer desktop. Thanks for the codes. I'll try it out too.

Similar Threads

  1. Selecting and dragging a image from multiple image in Java Applet
    By CY5 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2013, 02:44 PM
  2. Replies: 5
    Last Post: October 17th, 2011, 07:43 AM
  3. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  4. JFrame not repainting
    By dumb_terminal in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2010, 08:51 AM
  5. repainting a jframe containing two jpanels
    By musasabi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2010, 10:31 PM