Solved

How to update a JPanel with an image that is refreshed every second

Posted on 2008-06-11
12
699 Views
Last Modified: 2013-12-14
I have a JPanel within a JFrame in a basic swing application. I need to update the panel by redrawing an image that changes every second at the same location / URL (this is from an IP Camera). The application reads the image every second, but does not refresh the panel with the new image. I'm currently trying to use the 'update' method of the JPanel class for this task.
0
Comment
Question by:jsonburke
  • 6
  • 4
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 21764925
use a timer that calls repaint() every minute

0
 
LVL 92

Expert Comment

by:objects
ID: 21764935
why doesn't the app repaint the image after it reads the image?

0
 

Author Comment

by:jsonburke
ID: 21764980
I may be mistaken, but doesn't the update method call the repaint method? Or, is it vice versa?

Why I'm seeing this behavior is really a part of my question... my *guess* is because the image is at the same location so it is using a version cached in memory somewhere
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 92

Expert Comment

by:objects
ID: 21764988
how are you currently updating it?

0
 

Author Comment

by:jsonburke
ID: 21765015
This is the paint method I'm overriding in a separate jar at the moment just to get the update functionality to work as a start.

(Ultimately I will need to do some multi-thread work for the subpanel to display the feed without freezing the app once it connects)
public void paint(Graphics g) {
        // show the number of calls to paint()
        System.out.println(++n);
        // if there is an image and drawing has not finished
        // note: if the image is null drawImage will return true
        if (image == null ||!g.drawImage(image,0,0,getWidth(),getHeight(),this )) {
            // draw the message
            g.drawString(msg,20,getHeight()/2);
            System.out.println(msg);
        }
        
        if (n < 30) {
        try {
            Thread.sleep(1000);
            this.update(this.getGraphics());
        } catch (InterruptedException ex) {
            Logger.getLogger(ImageViewTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        } else {
            System.exit(0);
        }
    }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 21765021
repaint() will trigger a call to update()
You typically shouldn't be calling update() directly.

http://java.sun.com/products/jfc/tsc/articles/painting/

Sounds like the problem though is with the actual image loading.
0
 
LVL 92

Expert Comment

by:objects
ID: 21765040
       if (n < 30) {
        try {
            Thread.sleep(1000);
            this.update(this.getGraphics());
        } catch (InterruptedException ex) {
            Logger.getLogger(ImageViewTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        } else {
            System.exit(0);
        }


that code should not be in your paint method, it'll block your gui (swing is single threaded)

What class are you subclassing? You probleble should be overriding paintComponent() instead of paint()

A Jlabel can actually be used to display an image, without need for subclassing.

0
 

Author Comment

by:jsonburke
ID: 21765083
That code was from a runnable JPanel....

Could you elaborate on your thoughts of using paintComponent with a JLabel? And, what might be a better way to execute this repetition?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 21765154
JLabel already supports image display via the setIcon() method, no need for subclassing.

http://java.sun.com/docs/books/tutorial/uiswing/components/label.html

0
 

Author Comment

by:jsonburke
ID: 21765206
I will try this solution tomorrow and continue to read those docs in the mean time. Thank you for the direction thus far.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I. Introduction In a previous article (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_6537-PaperPort-Upgrade-How-to-download-and-install-updated-versions-of-PaperPort-11-and-12.html) (now deprecated), I discussed how to upgrad…
In a previously published article (http://www.experts-exchange.com/articles/10331/Automatic-Duplex-Scanning-in-PaperPort-Versions-11-12-14.html) here at Experts Exchange, I explained how to achieve duplex (double-sided) scanning in Nuance's PaperPor…
The goal of the tutorial is to teach the user how to remove a natural vignette your photos and how to add vignette to your photos.
Learn how to automatically add page numbers in your next InDesign project. This can be very helpful in multi-page books and magazines that you are designing. Make sure your Pages window visible.:  In the document you wish to add page numbers to. Act…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question