Solved

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

Posted on 2008-06-11
12
683 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article discusses the PaperPort 14 Scanner Connection Tool, which Nuance provides at no charge in order to fix scanning problems in Windows 8. Furthermore, users of PaperPort 14 in Windows 7 and Windows 10 have reported that the tool works in t…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
The goal of the tutorial is to teach the user how to edit a basic portrait of someone. This tutorial shows how to edit two basic photographs, one of a car and one of a person.
The goal of the tutorial is to teach the user how to import photos into Adobe Lightroom efficiently and to keep everything organized.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now