Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > paint vs update, Small Swing component to display an image

Reply
Thread Tools

paint vs update, Small Swing component to display an image

 
 
Bob
Guest
Posts: n/a
 
      11-28-2006
Hi there,

after seeking advice in this group, I wrote a small Swing component
that displays an image, assigned to it by another class.
I was amazed on how easy this was. Below is the simplified code.

Since the Component is automatically an ImageObserver, the picture on
the screen updates, when the image object is changed.
However, I am updating the image quite quickly in a simulation loop and
the update of the screen is not enough (or so fast, that it cannot be
seen). Is there any method that I can call in the simulation loop which
will trigger an update of the screen?
Furthermore, the paint() method doesn't seem to be called often enough.
For example, after the component has been hidden by a menu or
something, it won't be repainted. What's the point of repaint, if there
is no Graphics provided to paint on?

thanks in advance for any help
Bob

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

public class ImageDisplay extends Component
{
public Image image;
@Override
public void paint(Graphics g)
{
// super.paint(g);
if (image!=null)
{
g.drawImage(image, 0, 0, this);
}
} // paint()
} // class

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-28-2006
Bob wrote:
> Hi there,
>
> after seeking advice in this group, I wrote a small Swing component
> that displays an image, assigned to it by another class.
> I was amazed on how easy this was. Below is the simplified code.
>
> Since the Component is automatically an ImageObserver, the picture on
> the screen updates, when the image object is changed.
> However, I am updating the image quite quickly in a simulation loop and
> the update of the screen is not enough (or so fast, that it cannot be
> seen). Is there any method that I can call in the simulation loop which
> will trigger an update of the screen?
> Furthermore, the paint() method doesn't seem to be called often enough.
> For example, after the component has been hidden by a menu or
> something, it won't be repainted. What's the point of repaint, if there
> is no Graphics provided to paint on?
>
> thanks in advance for any help
> Bob
>
> import java.awt.Component;
> import java.awt.Graphics;
> import java.awt.Image;
> import java.awt.Rectangle;
>
> public class ImageDisplay extends Component
> {
> public Image image;
> @Override
> public void paint(Graphics g)
> {
> // super.paint(g);
> if (image!=null)
> {
> g.drawImage(image, 0, 0, this);
> }
> } // paint()
> } // class
>


If what you wrote is a Swing component then the code above is totally
useless. You don't call paint() on a Swing component, you need to call
paintComponent(). The ImageProducer/Consumer model is great for single
images but if you want to do animation you will need a more
sophisticated approach. Depending on how large your image and how often
you need to redraw it there are several different techniques that can be
employed. The fastest is probably active rendering but you lose the
system based repaints in that case.

If you want a real answer, give us a better description of what you are
actually doing.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to paint (using paint() in JPanel) inside mouse listeners eshedz@gmail.com Java 4 10-23-2005 09:13 PM
Swing component to display String[] Steve Java 4 10-17-2005 08:05 AM
Re: Swing popup frame doesn't paint. neshort@yahoo.com Java 0 03-07-2005 03:03 PM
equivalent for awt paint method in swing? Jochen Opp Java 2 09-25-2003 01:19 PM
equivalent for awt paint method in swing? Jochen Opp Java 0 09-24-2003 02:03 PM



Advertisments