2

我正在尝试使用半透明的 JFrame 在 Java 中制作动画。我在此处修改了 Oracle Java 教程中的演示代码。特别是 GradientTranslucentWindowDemo。

以下代码在 Windows XP SP3 最高 8 和 Mac OS X Mountain Lion 中运行良好,甚至在大多数情况下在 Linux 中运行良好。 Linux 中的问题,我需要帮助的是动画闪烁。

我正在运行带有 nVidia 驱动程序、Metacity 和 Compiz 的 Ubuntu Linux 12.04 LTS 64 位。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 上禁用了双缓冲。

有一个半透明的窗口必需的。

4

0 回答 0