简体   繁体   English

如何停止Java Applet中的图像闪烁?

[英]How can I stop image flickering in Java Applet?

My problem is that whenever I use a Java Applet I just coded, my png images flicker repeatdly. 我的问题是,每当我使用刚刚编码的Java Applet时,我的png图像就会反复闪烁。 I have already changed my thread sleep time to smaller, and that does not remove the problem. 我已经将线程的睡眠时间更改为更短的时间,但这并不能解决问题。

Here is the code where I'm calling on the image to be shown: 这是我在要显示的图像上调用的代码:

    public void init(){
    setSize(854, 480);
    Thread th = new Thread(this);
    th.start();
    offscreen = createImage(854,480);
    d = offscreen.getGraphics();
    addKeyListener(this);
}
public void paint(Graphics g){

    g.clearRect(0, 0, 854, 480);
    g.drawImage(background, 0, 0, this);
    d.drawImage(offscreen, 0, 0, this);
    g.drawOval(x, y, 20, 20);
}
public void update(Graphics g){
    paint(g);
}

And this line of code is where I'm declaring the image if you like 如果您愿意,这行代码是我声明图像的地方

    background = ImageIO.read(this.getClass().getResource("background.png"));

The flickering is caused by clearRect. 闪烁是由clearRect引起的。

If your images cover entire applet, you can just remove clearRect call. 如果您的图像覆盖了整个applet,则只需删除clearRect调用即可。

If they do not cover, you need to create an offscreen image, make all drawing to it, and then draw offscreen image by drawImage call to your applet. 如果它们没有覆盖,则需要创建一个屏幕外图像,对其进行所有绘制,然后通过对applet的drawImage调用绘制屏幕外图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM