Keilly: Full Screen Widgets

Tuesday, May 22, 2007

Full Screen Widgets

It is possible to have widgets that can enter a full screen mode. This would be useful for my ZX Spectrum widget where the user might want to quickly step back in time and pretend for a while that their $1000 Macbook is a humble 48K ZX Spectrum.

The only catch is that your useful widget code must be written in Java.

Java has the immensely useful Java full screen mode. The catch here is that an applet by default doesn't have enough permissions to use it. We can work around this by running as a application and starting Java from the command line rather than embedded in a web page. For full screen mode the rendering must be done to the contents of a Frame or Window.
So if you'd like to work in two modes: one as the contents of a small widget area in Dashboard and also as optionally full screen: write your code so it can be started as an applet and as an application.

Invoking full screen is easy ('this' is a JFrame containing our UI):

GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
setUndecorated(true);
gs[0].setFullScreenWindow(this);
setVisible(true);

We also want to exit full screen when the user hits 'Escape', I added this to the JFrame:

protected void processKeyEvent(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}

To run this from a Dashboard widget, use the widget.system command to run the application. Ensure that 'allow command line access' and 'allow java execution' is turned on in the widget preferences.

widget.system("/usr/bin/java -jar
FullScreenTest.jar fullscreentest.Main", null);

Boom! and that's it.

I've bundled this up into a test widget to demo the experience:

Widget: Full Screen Test
Java source: fullscreentest.Main

No comments: