views:

199

answers:

1

I have a Java JApplet embedded in a web page. The JApplet window contains a single instance of a class that extends JPanel - same size as the JApplet. The applet can spawn pop-up windows (JFrames) when the user clicks a button that's in the panel.

Every time I click on a button to pop-up a window, my applet flickers as it repaints. It also does the same when I click on it again or when it had focus and I click on a different window - my conclusion: it's a repaint that's being forced on focusLost() and focusGained() events.

I am implementing double buffering in the panel's paint() method like this:

@Override
public void paint(Graphics g)
{
    if(resized)
    {
        offscreen = createImage(getWidth(),getHeight());
        resized = false;
    }

    Graphics offscreenG = offscreen.getGraphics();

    /// DRAW HERE:

    // paint the main window contents:
    view.paint(offscreenG);
    // paint the child components of our panel.
    super.paint(offscreenG);

    /// FRAW FINISHED

    g.drawImage(offscreen,0,0,this);
    offscreenG.dispose();
}

The view object is not a swing component but just a class that knows hot to draw everything onto a Graphics object.

The JApplet's paint() method is not overridden.

I could probably override the focusGained/focusLost methods of my JApplet to prevent repainting - but I would rather hear a better solution to the problem.

+1  A: 

There is no need to implement double buffering, this is done automatically by Swing. You build an applet the same way you build an application. That is you add components to the content pane of the JApplet.

If you must do custom painting then you override the paintComponent() method of a JPanel and add the panel to the applet.

Read the section from the Swing tutorial on Custom Painting for examples.

camickr
thank you - very helpful!
Warlax