繁体   English   中英

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

[英]Fast changing Images / Frames (visible for 20ms)

我正在尝试创建一个程序,该程序显示1000毫秒的透明屏幕,然后显示20毫秒的图像。 那应该继续循环(1000ms-> 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();
            }
        });
    }
}

不知道它的闪烁速度是否更快,但是我能想到的就是:

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM