1

I'm writing a JApplet right now, and whenever I call super.paint(), the applet flickers. I am using double buffering (drawing to an image, and then rendering that image), but I think super.paint() is clearing the screen or something, defeating my double buffer.

I know I'm supposed to use paintComponents(), but for some reason, when I call "currentScreen.Draw(g)," it won't show the screen's draw.

Can anyone help me with this?

public void paint(Graphics g)
{   

    super.paint(g);//Remove this and it works, but the JApplet background color will be gone, and everything will be white.

    currentScreen.Draw(g);
}

Screen Draw Method

public void Draw(Graphics g)
{

    if(buffer != null)
        g.drawImage(buffer, 150, 0, null);
    //g.drawString(drawstring, x, y);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user830713
  • 155
  • 2
  • 6
  • Don't use double buffering, unless you really have to. JPanels are already double buffered by default. Also, yes, you should use paintComponents(). – iAndr0idOs Aug 10 '11 at 01:16
  • @iAndrOidOs: paintComponents is an entirely different and inappropriate method to use. `paintComponent` is what the OP should use instead. – Hovercraft Full Of Eels Aug 10 '11 at 01:19

1 Answers1

5

Don't use paint and don't draw directly in the JApplet. Instead draw in a JPanel's paintComponent method and call super.paintComponent(g) as the first line of that method. Add that JPanel to your JApplet's contentPane to allow the applet to display it.

Edit 1
Also you can't use paintComponents for this as this does something entirely different. Again use paintComponent but only in a component that derives from JComponent such as a JPanel (or a JComponent itself).

Edit 2 Also always put an @Override above your paintComponent method to be sure that you are in fact overriding the super method.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Unfortunately, if I use paintComponents, my "Screen" class won't draw. It's essentially a class with an Init(), Update(Graphics), and Draw(Graphics) functions. It also has two variables, Image buffer and Graphics buffer_g, which are the buffer variables for the screen. In the draw function, it just draws the buffer, and when the mouse is clicked/dragged, it adds lines to the buffer. When using paitnComponents, this doesn't work. – user830713 Aug 10 '11 at 03:04
  • @user830713: Please **re-read** my answer. Did you see my comment about not using "paintComponents"? – Hovercraft Full Of Eels Aug 10 '11 at 03:09
  • Sorry, I missed the "use a JPanel" part. It works perfectly now! Thanks! – user830713 Aug 10 '11 at 03:44
  • 1
    paintComponent`s`, @Override +1 – mKorbel Aug 10 '11 at 07:08