问题
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);
}
回答1:
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.
来源:https://stackoverflow.com/questions/7004866/japplet-super-paint-causes-flicker