Solved

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

Posted on 2008-06-11
12
712 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

PaperPort is a popular document imaging/management product from Nuance Communications (http://www.nuance.com/), previously known as ScanSoft. PaperPort is in widespread use by both individuals (http://www.nuance.com/for-individuals/by-product/paperp…
In a previous article here at Experts Exchange (http://www.experts-exchange.com/articles/18414/Create-a-PDF-file-with-Contact-Sheets-montage-of-thumbnails-for-all-JPG-files-in-a-folder-and-each-of-its-subfolders-using-an-automated-batch-method.html)…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

623 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