透明背景 JFrame Linux 上的动画

2023-11-25

我想为 Frame(或 JFrame)创建完全透明的背景并让它显示透明动画。我设法让它在 Windows 7 x64 中运行,但相同的代码无法在我的 Linux (Lubuntu x64 15.04) 上运行。

下面的代码显示了我想要实现的目标——只需复制并粘贴它即可。我只想让小矩形在屏幕上移动而不留下痕迹。

static int  a   = 0;

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setUndecorated(true);
    f.setBackground(new Color(0, 0, 0, 0));
    f.setVisible(true);
    f.setSize(512, 512);
    f.add(new JPanel() {
        @Override
        public void paintComponent(Graphics gr) {
            Graphics2D g = (Graphics2D)gr;
            g.setBackground(new Color(0, 0, 0, 0));
            g.clearRect(0, 0, 512, 512);
            g.drawRect(a, a++, 2, 2);
        }
    });

    while(true) {
        try {
            Thread.sleep(30);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        f.repaint();
    }
}

我想要实现的目标(如 Windows 中所示)以及我通过 Lubuntu 15.04 获得的目标:

Desired Animation          Lubuntu Animation

我只想看到小方块的移动,就像 Windows 7 上显示的那样 - 我不想看到痕迹。

请不要给我 Oracle 透明度和窗口文档的链接——我已经检查了三次。

我尝试过的:

  • Graphics2D 的透明空间的“copyArea()”。 (据我所知,这曾经有效,但现在不再有效)
  • 玻璃板
  • 阿尔法复合材料
  • 设置绘制()

请先测试一下你的想法/代码。我已经尝试过很多“这应该有效”的东西,但似乎并没有......非常感谢所有帮助。


作为参考,这里有一个最小的完整的例子,适合跨平台测试。注意

  • 在某些平台上,例如乌班图,一个完全地透明背景不被视为opaque;一个小的、非零的alphavalue 是一种典型的解决方法。

  • Swing GUI 对象应该被构造和操作only on the 事件派发线程.

  • Use java.swing.Timer,它在事件调度线程上运行,以调整动画速度。

  • 不要使用setPreferredSize()当你真正想要覆盖时getPreferredSize().

image

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * @see https://stackoverflow.com/a/31328464/230513
 */
public class TransparentAnimation {

    private static final Color tranparentBlack = new Color(0, 0, 0, 1);

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setUndecorated(true);
        f.setBackground(tranparentBlack);
        f.add(new JPanel() {
            int x, y;
            Timer t = new Timer(10, (ActionEvent e) -> {
                x = (x + 1) % getWidth();
                y = (y + 1) % getHeight();
                repaint();
            });

            {
                setBackground(tranparentBlack);
                t.start();
            }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                g.fillOval(x, y, 16, 16);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        });
        f.add(new JLabel(System.getProperty("os.name") + "; v"
            + System.getProperty("os.version")), BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new TransparentAnimation()::display);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

透明背景 JFrame Linux 上的动画 的相关文章

随机推荐