在Windows中,我如何以编程方式使我的整个屏幕闪烁黑色&白色?

时间:2012-04-21 02:26:17

标签: java winapi timer

我正在使用一个简单的番茄钟计时器,但我没有声音/扬声器,所以我想通过简单地用指定的颜色消隐屏幕,然后用另一种颜色切换几次来发出计时器的结束信号。好像我让我的显示器左转车信号。我该如何控制显示器?是否有一些win32代码?

我熟悉Java,而不熟悉c#或C ++

enter image description here

1 个答案:

答案 0 :(得分:7)

我不确定控制整个屏幕的动机是什么。也许更合适的是在系统托盘中显示通知。

无论如何,使用Java可以在全屏模式下弹出JFrame。使用计时器多次更改其背景,然后关闭它。这是一个演示类似的简化示例:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

class FullScreenFrame extends JFrame{
    private static final int MAX_COUNT = 3;
    private static final int PERIOD = 1000;
    private JPanel panel;
    private int count;

    public FullScreenFrame(){
        super();

        setUndecorated(true);

        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), "ESCAPE");
        getRootPane().getActionMap().put("ESCAPE", new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        panel = new JPanel();
        panel.setBackground(Color.GREEN);
        add(panel);

        new Timer(PERIOD, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (count < MAX_COUNT) {
                    if (count % 2 == 0){
                        panel.setBackground(Color.BLACK);           
                    } else {
                        panel.setBackground(Color.GREEN);
                    }
                    count++;

                } else {
                    ((Timer) e.getSource()).stop();
                    dispose();
                }
            }

        }).start();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                FullScreenFrame frame = new FullScreenFrame();
                frame.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds());
                frame.setAlwaysOnTop(true);
                frame.setVisible(true);
            }
        });
    }
}