fnL
fnL

Reputation: 43

Java - Put screen to sleep (or screensaver)

I'm currently writing a program for our local fire department to display the firetrucks that have to be occupied, the destination of the alarm etc.

The program itself is written in Java and runs on a raspberry pi connected to a TV screen. In order to protect the screen from staying on the same site and content and prevent the screen from burning in I want to put the screen into sleep or turn the screensaver on when the alarm is over (command will be sent over TCP, but that's not the problem). I also want to turn the screen back up when a new alarm is received.

I have not been able to find a solution that will put the screen to sleep or screensaver, and get it back up again. Has anyone done something like this?

Upvotes: 4

Views: 1290

Answers (3)

Aaron Esau
Aaron Esau

Reputation: 1123

Unfortunately, Java does not have a built-in library for this. However, you can do

Runtime.getRuntime().exec("xscreensaver-command -activate");

To deactivate the screensaver, do

Runtime.getRuntime().exec("xscreensaver-command -deactivate");

Also, make sure xscreensaver is installed

sudo apt-get install xscreensaver

I don't know if this will help reduce the heat from the screen, but you could try

public class ScreenSaver {
    private static final JFrame frame = new JFrame();

    public static void startScreenSaver() throws Exception {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         frame.setBackground(Color.BLACK);
         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         frame.setResizable(false);
         frame.setUndecorated(true);
         frame.validate();
         GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);
         frame.setVisible(true);
    }

    public static void stopScreenSaver() {
         frame.setVisible(false);
    }
}

Upvotes: 3

Durandal
Durandal

Reputation: 20069

There is no way to do this from plain java. You will either need to resort to platform specific API's (using JNI) or if available to command line utilities specific to the platform.

Emulating a screensaver yourself, while possible, will only waste more power (you're actively drawing something wasting CPU/GPU power, and the screen will not go into standby).

It may be best to drop the idea to control the screen from you application. Instead leave that to the operating system - any platform should be easily set up to turn off the screen after some time without user input.

Upvotes: 0

Josh
Josh

Reputation: 187

I don't think java allows you to control native functions on your computer such as sleep or standy.

There are a few options you can take:

1) If your program will always be in focus, you can have a thread that keeps track of how long your program hasn't been touched and then you can activate a "screensaver" in your program.

2) You could use the JNA to access the computers native functions and have it set to display the screensaver or go to sleep there.

Upvotes: -1

Related Questions