【问题标题】:Java Applet - How to add double-buffering to JButtonJava Applet - 如何向 JButton 添加双缓冲
【发布时间】:2018-05-17 03:26:15
【问题描述】:

我目前正在使用 Applet 类来创建一个简单的游戏。因为有闪烁效果,所以我通过创建像这样的屏幕外缓冲区为Graphics 组件添加了双缓冲:

public class AppletTest extends Applet implements Runnable {

    Thread thread;
    Image img;
    Graphics gfx;

    public final int WIDTH = 700, HEIGHT = 500;

    public void init() {
        this.resize(WIDTH, HEIGHT);

        thread = new Thread(this);
        thread.start();

        img = createImage(WIDTH, HEIGHT); // off-screen buffering
        gfx = img.getGraphics();
    }
    public void draw(Graphics g) {
        gfx.setColor(Color.BLACK);
        gfx.fillRect(0, 0, WIDTH, HEIGHT);
        gfx.setColor(Color.WHITE);

        gfx.fillRect(50, 50, 100, 100);
        gfx.setFont(new Font("Century", Font.BOLD, 30));
        gfx.drawString("I feel good sometimes I don't", 200, 200);            

        g.drawImage(img, 0, 0, this); // draws the off-screen image
    }
    public void update(Graphics g) {
        draw(g);
    }

    public void run() {
        while(true) {
            repaint();
            try {
                Thread.sleep(5);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

如果您运行应用程序,所有Graphics(.fillRect、.drawString 等)组件/方法都将绘制在屏幕外缓冲区上。但是,我的目标是向小程序添加一个 JButton - 正如预期的那样,JButton 组件没有离屏加载(这意味着闪烁)。

Graphics gfx;
JButton button1;

public void draw(Graphics g) {
    setLayout(null);

    button1.setBounds(225, 400, 250, 50);
    button1.setFont(new Font("Courier", Font.PLAIN, 17));
    button1.setForeground(Color.WHITE);
    button1.setBackground(Color.DARK_GRAY);

    add(button1); // is it possible to draw the JButton on the off-screen buffer?
}

如何将屏幕外加载添加到 JButton 组件?

【问题讨论】:

  • Swing 组件已经是双缓冲的,问题是,您不尊重小程序绘制链(通过不调用 super.update 并将其传递给您的“双缓冲”)。话虽如此,小程序已经死了 - 是时候继续前进了。更好的解决方案是从已经双缓冲的JPanel 开始。然后你可以将它添加到你想要的任何容器中。另外,不要尝试“绘制” Swing 组件,除了绘制它们之外,还有很多事情要做
  • 根据这个旧的 SO 帖子,JPanel 中可能仍需要双缓冲。不确定这是否有帮助,但 OP 与使用 Jpanel 时闪烁的问题相同。链接:stackoverflow.com/questions/2063607/java-panel-double-buffering
  • @KrishnanshuGupta OP(链接问题)存在闪烁问题,因为它们违反了绘画系统。如果使用正确,Swing 组件默认是双缓冲的

标签: java applet jbutton double-buffering


【解决方案1】:

Applet(和JApplet)已正式弃用,Java、Oracle、浏览器(或一般社区)不再支持它们

默认情况下,Swing 组件是双缓冲的。如果您正确使用绘画系统,您应该不会遇到任何闪烁,如果您这样做,则很明显表明您做错了。

我建议您查看 Performing Custom PaintingPainting in AWT and Swing 以了解有关 Swing 绘画系统如何工作的更多详细信息。

Swing 是单线程的,不是线程安全的。这意味着您不应在事件调度线程的上下文中执行任何长时间运行的操作,并且不应从 EDT 的上下文之外更新 UI。

查看Concurrency in Swing了解更多详情。

解决这些问题的一个简单方法是使用 Swing Timer,它可用于安排在 EDT 上下文中执行的定期更新。

更多详情请见How to Use Swing Timers...

作为一个基本的可运行示例...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        public static final int WIDTH = 700, HEIGHT = 500;

        public TestPane() {
            setLayout(new GridBagLayout());
            add(new JButton("Big fat button"));
            Timer timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(WIDTH, HEIGHT);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, WIDTH, HEIGHT);
            g2d.setColor(Color.WHITE);

            g2d.fillRect(50, 50, 100, 100);
            g2d.setFont(new Font("Century", Font.BOLD, 30));
            g2d.drawString("I feel good sometimes I don't", 200, 200);

            g2d.dispose();
        }

    }

}

好的,“但是我绝对,必须,不问任何问题,使用Applet ...只需创建JPanel 的实例并添加到Applet 容器即可轻松应用于J/Applet

Swing 使用“被动渲染”算法,如果你绝对必须完全控制,那么你可以看看BufferStrategy 将绘画系统的完全控制权交给你,但你不能使用 Swing 组件,因为它们由 Swing 子系统更新

【讨论】:

    猜你喜欢
    • 2015-03-17
    • 1970-01-01
    • 2012-08-27
    • 2013-01-07
    • 2011-08-20
    • 2012-03-22
    • 2011-01-05
    • 2013-08-07
    • 2011-02-20
    相关资源
    最近更新 更多