paint vs update, Small Swing component to display an image

B

Bob

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
 
K

Knute Johnson

Bob said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,970
Messages
2,570,162
Members
46,710
Latest member
bernietqt

Latest Threads

Top