快速变化的图像/帧(可见20ms)

时间:2017-12-05 06:09:23

标签: java image swing

我试图创建一个程序,显示1000毫秒的透明屏幕跟随显示20毫秒的图像。这应该在循环中继续(1000m s - > 20 ms - > 1000 ms - > 20 ms - > ....)。

显示图像的时间太长,因此如果出现,您可以在屏幕上看到图像。

有没有办法获得更好的表现?

以下是代码:

import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.util.TimerTask;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


public class SubPrimingJava extends JFrame {
    public SubPrimingJava() {
        super("GradientTranslucentWindow");

        setBackground(new Color(0,0,0,0));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width+20, screenSize.height+200);
        //setLocationRelativeTo(null);
        setLocation(-10, -100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                if (g instanceof Graphics2D) {
                    final int R = 240;
                    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, 0), true);
                    Graphics2D g2d = (Graphics2D)g;
                    g2d.setPaint(p);
                    g2d.fillRect(0, 0, getWidth(), getHeight());
                }
            }
        };
        setContentPane(panel);
        setLayout(new GridBagLayout());




    }

    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() {
                SubPrimingJava gtw = new
                    SubPrimingJava();

                // Display the window.
                gtw.setVisible(true);
                ImageIcon icon = new ImageIcon("test.png");
                JLabel label = new JLabel( icon );
                gtw.add(label);
                gtw.setOpacity(0);

                Timer timer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        gtw.setOpacity(1);
                        Timer t2 = new Timer(1, new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                gtw.setOpacity(0);
                            }
                        });
                        t2.start();
                        t2.setRepeats(false);
                    }
                });
                timer.setRepeats(true);
                timer.start();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

不知道这是否会更快地闪烁,但我能想到的只有:

Timer timer = new Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {

        gtw.setOpacity(1);
        label.paintImmediately(label.getBounds());

        gtw.setOpacity(0);
        label.paintImmediately(label.getBounds());
    }
});
timer.setRepeats(true);
timer.start();