问题描述
我现在正在编写一个JApplet,每当我调用super.paint()时,applet都会闪烁。
我正在使用双缓冲(绘制到图像,然后渲染该图像),但我认为super.paint()正在清除屏幕或其他东西,击败我的双缓冲区。
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.
我知道我应该使用paintComponents(),但出于某种原因,当我调用currentScreen.Draw(g)时,它不会显示屏幕的绘制。
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.
任何人都可以帮我吗?
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);
}
屏幕绘制方法
public void Draw(Graphics g)
{
if(buffer != null)
g.drawImage(buffer, 150, 0, null);
//g.drawString(drawstring, x, y);
}
推荐答案
不要使用油漆和不要直接在JApplet中绘制。而是在JPanel的paintComponent方法中绘制并调用super.paintComponent(g)作为该方法的第一行。将JPanel添加到JApplet的contentPane中以允许applet显示它。
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.
编辑1
此外你还可以'为此使用paintComponent s ,因为这会完全不同。再次使用paintComponent但仅在从JComponent派生的组件中,例如JPanel(或JComponent本身)。
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).
编辑2
还总是将@Override放在paintComponent方法之上,以确保实际上覆盖了super方法。
Edit 2Also always put an @Override above your paintComponent method to be sure that you are in fact overriding the super method.
这篇关于JApplet - super.paint();导致闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!