Solved

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

Posted on 2008-06-11
12
709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.
The goal of the tutorial is to teach the user how to import photos into Adobe Lightroom efficiently and to keep everything organized.

710 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