在Java中实现双缓冲 [英] Implementing Double Buffering in Java

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

问题描述

我有一个简单的Java JFrame画布。我每隔半秒左右更新屏幕上的内容,并闪烁。我想实现双缓冲来消除闪烁,但我对Java相当陌生,并且不熟悉如何操作。我找到了一些例子,但不知道如何实施他们的方法到我的。

以下是我现在拥有的东西的基本设置。这不是我的确切代码 - 只是基本设置的一个例子。



感谢您的正确推动!

  public class myCanvas扩展Canvas {
//变量
颜色rectColor = Color.red;

public myCanvas()
{
}

public void paint(Graphics graphics)
{
//初始设置,例如
graphics.setColor(rectColor);
graphics.fillRect(X,Y,W,H);

public static void main(String [] args)
{
myCanvas canvas = new myCanvas();
JFrame frame = new JFrame(GUI);
frame.setSize(frameWidth,frameHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane()。add(canvas);
frame.setVisible(true);
while(true){
rectColor = Color.green;
canvas.validate();
canvas.repaint();
Thread.sleep(500);




解决方案这里,如果你想知道更多的话)。 Canvas ,您可以使用 JPanel 。这也给了你一个潜在的解决方案,因为 JPanel 有一个方法 setDoubleBuffered(boolean),更具体地说,这是实现的在 JComponent 类中。



我相信只要替换
就足够了

  public class myCanvas extends Canvas 

by

  public class myCanvas extends JPanel 

。虽然我还没有测试过,但我希望它能帮助你!

编辑:
当然,在设置框架 canvas main 方法中,您必须将方法调用

  canvas.setDoubleBuffered(true); 

某处。


I have a simple Java JFrame canvas. I am updating what is on the screen every half second or so, and have flickering. I want to implement double buffering to eliminate the flickering, but I am fairly new to Java and am unfamiliar with how to do so. I have found some examples, but not sure how to implement their methods into mine.

Below is the basic setup of how I have things now. This is not my exact code- just an example of the basic setup.

Thanks for any push in the right direction!

public class myCanvas extends Canvas{
    //variables
    Color rectColor=Color.red;

    public myCanvas()
    {
    }

    public void paint(Graphics graphics)
    {
        //initial setup, such as
        graphics.setColor(rectColor);
        graphics.fillRect(X,Y,W,H);
    }
    public static void main(String[] args)
    {
        myCanvas canvas = new myCanvas();
        JFrame frame = new JFrame("GUI");
        frame.setSize(frameWidth,frameHeight);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(canvas);
        frame.setVisible(true);
        while(true){
            rectColor=Color.green;
            canvas.validate();
            canvas.repaint();
            Thread.sleep(500);
        }
    }
}

解决方案

First of all, you should avoid mixing heavy- and lightweight components (AWT and SWING), mostly because they use very different methods of drawing to the display (read here if you want to know more).

So instead of the Canvas, you could use a JPanel. This also gives you a potential solution, because JPanel has a method setDoubleBuffered(boolean), more specifically, this is implemented in the JComponent class.

I believe it would be sufficient to just replace

public class myCanvas extends Canvas

by

public class myCanvas extends JPanel

. Although I haven't tested this, I hope it helps you!

EDIT: Also, of course, when setting up your frame and canvas in the main method, you'd have to place the method call

canvas.setDoubleBuffered(true);

somewhere.

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

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