Java Applet - 如何为 JButton 添加双缓冲

标签 java applet jbutton double-buffering

我目前正在使用 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 组件添加离屏加载?

最佳答案

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 ...😓,那么我为你感到遗憾,但这并不能改变 Swing 已经是双缓冲的事实。只需创建 JPanel 的实例并添加到 Applet 容器,即可轻松将上面的示例应用于 J/Applet

Swing采用了“被动渲染”算法,如果你一定要完全掌控,那么你可以看看BufferStrategy它将绘画系统的完全控制权交给您,但您将无法使用 Swing 组件,因为它们是由 Swing 子系统更新的

关于Java Applet - 如何为 JButton 添加双缓冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50382897/

相关文章:

java - 将 JButton 放在右上角

java - JButton 仅在我将鼠标悬停在其上后才出现?

java - Spring Security 中的 AuthenticationSuccessHandler

java - WebJars 无法在 JSP 中访问

java - JEdi​​torPane.setpage 无法再与 java 1.6.22 正常工作

Java 小程序适用于 appletviewer、Safari 和 Firefox,但不适用于 Chrome

java - 关于使用 jsp 创建管理页面的建议

java - 启动 jnlp 文件时出错 : Could not create the Java Virtual Machine and A fatal exception has occurred. 程序将退出

java - Applet 可以打开新的 HTML 窗口*并*绕过弹出窗口阻止程序吗?

java - JButton 不会显示 SHORT_DESCRIPTION,setEnabled 也不起作用