?
Solved

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

Posted on 2008-06-11
12
Medium Priority
?
715 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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)…
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 …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The goal of the tutorial is to teach the user how to import photos into Adobe Lightroom efficiently and to keep everything organized.

719 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