paint vs update, Small Swing component to display an image

Discussion in 'Java' started by Bob, Nov 28, 2006.

  1. Bob

    Bob Guest

    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
    Bob, Nov 28, 2006
    #1
    1. Advertising

  2. 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/
    Knute Johnson, Nov 28, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jochen Opp
    Replies:
    0
    Views:
    359
    Jochen Opp
    Sep 24, 2003
  2. Jochen Opp
    Replies:
    2
    Views:
    662
    Chris Smith
    Sep 25, 2003
  3. VideoProcessor

    How to paint in Component Graphics?

    VideoProcessor, Nov 12, 2004, in forum: Java
    Replies:
    9
    Views:
    581
    Andrei Kouznetsov
    Nov 13, 2004
  4. Replies:
    0
    Views:
    4,323
  5. Replies:
    4
    Views:
    4,615
    Brendan Guild
    Oct 23, 2005
Loading...

Share This Page