Java窗口半透明动画在Linux上闪烁

时间:2013-03-29 14:10:04

标签: java linux swing translucency

我正在尝试用半透明的JFrame在Java中做动画。我已经修改了Oracle Java Tutorials here中的演示代码。特别是GradientTranslucentWindowDemo。

以下代码适用于最高8的Windows XP SP3和Mac OS X Mountain Lion,甚至Linux Linux中的问题,以及我需要帮助的是,动画是闪烁的。

我正在使用nVidia驱动程序,Metacity和Compiz运行Ubuntu Linux 12.04 LTS 64bit。 PERPIXEL_TRANSLUCENT报告为真,并且运行良好。

以下代码中是否存在我缺少的内容,或者我需要更改Linux方面的内容?我在JPanel上尝试了setDoubleBuffered(true),但它没有消除闪烁。

请将我的代码更改引用到以下演示:

import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Paint;
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.SwingUtilities;
import javax.swing.Timer;

public class GradientTranslucentWindowDemo extends JFrame implements ActionListener {
    private Timer timer = new Timer(100, this);

    private double percentage = 0.0;

    private JPanel surface = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (g instanceof Graphics2D) {

                final int R = 0;
                final int G = 240;
                final int B = 240;

                Paint p =
                    new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
                        0.0f, getHeight(), new Color(R, G, B, 255), false);
                Graphics2D g2d = (Graphics2D)g;
                // CHANGE 1
                // Clear the previous graphics using a completely transparent fill
                g2d.setBackground(new Color(0, 0, 0, 0));
                g2d.clearRect(0, 0, getWidth(), getHeight());
                g2d.setPaint(p);
                // CHANGE 2
                // Only do a gradient fill for the current percentage of the width
                g2d.fillRect(0, 0, (int)Math.ceil(getWidth() * percentage), getHeight());
            }
        }
    };

    public GradientTranslucentWindowDemo() {
        super("GradientTranslucentWindow");

        setBackground(new Color(0,0,0,0));
        setSize(new Dimension(300,200));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // CHANGE 3
        // I thought this might remove the flicker, nope
        this.surface.setDoubleBuffered(true);

        // CHANGE 4
        // This seems to be required or the g2d.clearRect doesn't do anything
        this.surface.setOpaque(false);
        setContentPane(this.surface);
        setLayout(new GridBagLayout());
        add(new JButton("I am a Button"));
    }

    // CHANGE 5
    // On each tick of the timer increment the percentage until its
    // more than one and always repaint
    @Override
    public void actionPerformed(ActionEvent event) {
        this.percentage += 0.05;
        if (this.percentage > 1.0) {
            this.percentage = 0.0;
        }
        this.surface.repaint();
    }

    public static void main(String[] args) {
        // Determine what the GraphicsDevice can support.
        GraphicsEnvironment ge = 
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        boolean isPerPixelTranslucencySupported = 
            gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);

        //If translucent windows aren't supported, exit.
        if (!isPerPixelTranslucencySupported) {
            System.out.println(
                "Per-pixel translucency is not supported");
                System.exit(0);
        }

        JFrame.setDefaultLookAndFeelDecorated(true);

        // Create the GUI on the event-dispatching thread
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GradientTranslucentWindowDemo gtw = new GradientTranslucentWindowDemo();

                // Display the window.
                gtw.setVisible(true);

                // CHANGE 6
                // Wait until the window is visible to start the timer
                gtw.timer.start();
            }
        });
    }
}

更新1: 删除半透明度并选择黑色背景可修复闪烁问题。闪烁肯定与具有半透明窗口有关。我还注意到随着窗口的扩大,闪烁变得更糟。

更新2:this.surface.setOpaque(false);是造成问题的原因。如果这被注释掉,动画不会闪烁,并且有半透明效果。 但是,在动画的每次迭代中,它都会与之前的颜色混合(在重新绘制之前不会清除内容)。除非设置g2d.setBackground(new Color(0, 0, 0, 0));,否则g2d.clearRect(0, 0, getWidth(), getHeight());this.surface.setOpaque(false);无效。它几乎就像这行禁用了linux上的双缓冲。

拥有半透明窗口的要求。

0 个答案:

没有答案