首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JWindow进行双缓冲

用JWindow进行双缓冲
EN

Stack Overflow用户
提问于 2015-08-20 07:40:32
回答 1查看 86关注 0票数 0

我试图双倍缓冲透明的JWindow,但是所使用的技术似乎没有任何效果(不同的循环值是相互绘制的)。

代码语言:javascript
复制
public final class Overlay extends JWindow {

    public static final Color TRANSPARENT = new Color(0, true);
    public static Font standardFont = null;

    public static Overlay open() {
        return new Overlay();
    }

    private Overlay() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setAlwaysOnTop(true);
        setBounds(0, 0, screenSize.width, screenSize.height);
        setBackground(TRANSPARENT);
    }

    @Override
    public void paint(Graphics g) {
        BufferedImage bufferedImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d = bufferedImage.createGraphics();
        paintUs(g2d);

        Graphics2D g2dComponent = (Graphics2D) g;
        g2dComponent.drawImage(bufferedImage, null, 0, 0);
    }

    private void paintUs(Graphics2D g) {
        int height = 420;
        int x = 20;
        g.setColor(TRANSPARENT);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setFont(standardFont == null ? standardFont = g.getFont().deriveFont(17f) : standardFont);
        for (Plugin plugin : Abendigo.plugins()) {
            g.setColor(Abendigo.plugins().isEnabled(plugin) ? Color.GREEN : Color.RED);
            g.drawString(plugin.toString(), x + 5, getHeight() - height);
            height += 20;
        }
        height += 20;
        g.setColor(Color.YELLOW);
        g.drawString("Cycle: " + Abendigo.elapsed + "ms", x, getHeight() - height);
    }

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

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-20 07:42:45

为什么!?Swing组件已经双缓冲了吗?简单地创建一个自定义组件,从类似JPanel的内容扩展,覆盖它的paintComponent,并在那里执行自定义绘画。确保将组件设置为透明(setOpaque(false)),并将其添加到JWindow实例中。

有关更多细节,请参见AWT和Swing中的绘画表演定制绘画

您面临的直接问题之一是Swing窗口已经附加了一系列复合组件(JRootPanecontentPane等),所有这些组件都可以独立于您的窗口绘制,这意味着它们可以覆盖您试图直接绘制到窗口上的每一个组件。相反,避免直接绘制到窗口,而是使用自定义组件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32112285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档