Java的JApplet的图形双缓冲 [英] Java JApplet Graphics Double Buffering

查看:288
本文介绍了Java的JApplet的图形双缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Java中的pretty简单的游戏,和我运行到非常严重的闪烁的问题,当我玩游戏是在浏览器中的小程序。即,所有我的精灵,目前正在上的背景的顶部绘的,有时会在屏幕上示出,但通常不是 - 它们反复闪烁到屏幕上,然后消失。我读过双缓冲可能是解决这个,但我有正确实现它的麻烦。

I'm writing a pretty simple game in Java, and I'm running into the issue of very serious flickering when I play the game as an applet in a browser. That is, all of my sprites, which are being painted on top of a background, are sometimes shown onscreen, but usually not - they repetitively flash onto the screen and then disappear. I've read that double buffering is probably the solution to this, but I'm having trouble implementing it correctly.

我使用JApplet的作为一个JPanel容器。此JPanel是在其上的精灵和游戏对象都画在容器 - 也就是说,在JPanel的paintComonent方法。在我JApplet的,我使用的初始化,油漆和更新重写方法如下:

I'm using a JApplet as the container for a JPanel. This JPanel is the container onto which the sprites and game objects are painted - that is, in the JPanel's paintComonent method. In my JApplet, I'm using init, paint, and update override methods as follows:

Image offscreen;

Graphics bufferGraphics;

Dimension dim;

public void init(){
    dim = getSize();

    setBackground(Color.BLACK);

    offscreen = createImage(dim.width,dim.height);

    bufferGraphics = offscreen.getGraphics();

}

public void paint(Graphics g){
    bufferGraphics.clearRect(0,0,dim.width,dim.height);
    //here is my question - i"m not sure what I should print to bufferGraphics
    g.drawImage(offscreen, 0, 0, this);
}

public void update(Graphics g){
    paint(g);
}

我遇到的问题是,在注释行,我不能确定是什么做的就是打印到bufferGraphics当前小程序的图像。我读了 的例子中,精灵被漆成直奔JApplet的,不使用的JPanel。鉴于此,我的猜测是,我需要在注释行的JPanel的油漆到bufferGraphics。我在这里在正确的轨道上?任何帮助是极大的AP preciated;我只是想知道什么办法可以正常做到这一点。

The problem I'm running into is that, at the commented line, I'm unsure of what to do to get the current applet image printed to bufferGraphics. I read an example in which the sprite was painted straight to the JApplet, without using a JPanel. In light of that, my guess is that I'd need to paint the JPanel onto bufferGraphics at the commented line. Am I on the right track here? Any help is greatly appreciated; I just would like to know any way to do this properly.

推荐答案

Swing是双默认缓冲,也没有必要做任何特别的。

Swing is double buffered by default, there is no need to do anything special.

您的问题可能是这幅画code。您发布的code用于AWT画,不可摆动画。

Your problem is probably the painting code. The code you posted is used for AWT painting, NOT Swing painting.

风俗画是通过覆盖一个JPanel或者JComponent中的的paintComponent()方法实现。我建议你​​阅读了Swing教程风俗画的开始工作示例。

Custom painting is done by overriding the paintComponent() method of a JPanel or JComponent. I suggest you start by reading the Swing tutorial on Custom Painting for a working example.

这篇关于Java的JApplet的图形双缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆