透明背景 JFrame Linux 上的动画
我想为 Frame(或 JFrame)创建一个完全透明的背景,并让它显示一个透明的动画.我设法让它在 Windows 7 x64 上运行,但相同的代码不能在我的 Linux (Lubuntu x64 15.04) 上运行.
I want to create a fully transparent background for a Frame (or JFrame) and have it show a transparent animation. I managed to get it working in Windows 7 x64 but the same code does not run on my Linux (Lubuntu x64 15.04).
下面的代码显示了我想要实现的目标——只需复制 &粘贴它.我只想让小矩形在屏幕上移动而不留下任何痕迹.
The code below shows what I'm trying to achieve--just copy & paste it. I just want the little rectangle to move across the screen without leaving a trail.
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 中得到的结果:
What I want to achieve (as shown in Windows) and what I get with Lubuntu 15.04:
我只想看到小方块移动,就像 Windows 7 上显示的那样——我不想看到踪迹.
I just want to see the little square move just like what's shown on Windows 7--I don't want to see a trail.
请不要给我 Oracle 的透明度和窗口文档的链接——我已经看了三次.
Please don't give me the link of Oracle's transparency and window documentation--I've gone over it all thrice.
我的尝试:
- Graphics2D 的透明空间的copyArea()".(这曾经适用于 AFAIK,但不再适用)
- 玻璃板
- AlphaComposite
- setPaint()
请先测试您的想法/代码.我已经尝试过很多这应该有效"的东西,但似乎没有……非常感谢所有帮助.
Please please just test out your thoughts/code first. A lot of the "this should work" stuff I have already tried and does not seem to... All help is greatly appreciated.
推荐答案
基本上这个问题是操作系统相关的.适用于 Windows 的方法不适用于 Linux,反之亦然.
Basically this issue is OS-related. What works for Windows will not work for Linux and vice versa.
由于某种原因,Linux 在设置 BufferStrategy 时只允许动画每像素透明度.但是,此解决方案在 Windows 上失败.因此,我提出了以下代码,它根据操作系统选择正确的算法:
For some reason, Linux only allows animated per-pixel-transparency when setting up a BufferStrategy. However, this solution fails on Windows. As a result I have come up with the following code which picks the correct algorithm based on the OS:
static int a = 0;
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel p = new JPanel() {
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setBackground(new Color(255, 255, 255, 0));
g2d.clearRect(0, 0, f.getWidth(), f.getHeight());
g2d.drawRect(a, a++, 2, 2);
}
};
f.add(p);
f.setUndecorated(true);
f.setBackground(new Color(255, 255, 255, 0));
f.setSize(512, 512);
f.setVisible(true);
f.createBufferStrategy(2);
BufferStrategy bs = f.getBufferStrategy();
while (true) {
try {
Thread.sleep(33);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (System.getProperty("os.name").contains("indows ")) {
p.repaint();
} else {
Graphics g = null;
do {
try {
g = bs.getDrawGraphics();
p.update(g);
} finally {
g.dispose();
}
bs.show();
} while (bs.contentsLost());
Toolkit.getDefaultToolkit().sync();
}
}
}
此代码适用于我的 Windows 7 x64 和 Lubuntu 15.04 x64.请亲自试用此代码,看看它是否适合您.我自己没有Mac,所以如果有人愿意为我测试一下,我将非常感激.如果对任何人都不起作用,请告诉我.
This code works for my Windows 7 x64 and my Lubuntu 15.04 x64. Please try out this code out yourself and see if it works for you. I myself don't own a Mac so if someone would please test it for me I would be very grateful. If it does not work for anyone, please let me know.
这是你应该看到的:
相关文章