Re: paint vs update, Small Swing component to display an image



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/
.



Relevant Pages

  • Re: program works but wont display in browser
    ... The paint() method of Swing component should ... override the paintmethod in your custom class. ... for you JApplet class. ...
    (comp.lang.java.help)
  • paint vs update, Small Swing component to display an image
    ... after seeking advice in this group, I wrote a small Swing component ... I am updating the image quite quickly in a simulation loop and ... the paint() method doesn't seem to be called often enough. ... What's the point of repaint, ...
    (comp.lang.java.programmer)
  • Re: JFrame paint
    ... and a swing component should call paintComponentfor ... JFrame and to this end overrode paint() in JFrame. ... The same effect happens when overriding paintComponent(): ...
    (comp.lang.java.programmer)
  • Re: Where can I go to understand lightweight vs heavyweight issues?
    ... As far as I can tell there is no way to say, "Draw the button, but only ... When I said repaint() or used the RepaintManager to repaint all dirty ... When I made the suggested changes to my menus, ... > would draw over the Swing component, ...
    (comp.lang.java.gui)
  • Re: Where can I go to understand lightweight vs heavyweight issues?
    ... When I said repaint() or used the RepaintManager to repaint all dirty regions of the button, the system always told me that the entire button needed to be repainted. ... When I made the suggested changes to my menus, the buttons stopped painting over the top of the menus even though the entire button was still being repainted. ... And even if you didn't, it is the heavyweight component that would draw over the Swing component, defeating the normal Z-ordering. ...
    (comp.lang.java.gui)