[GoogleWallpaper PIC] JavaArt Chapter 0.5.   Automatic Wallpapering

 

This chapter does not appear in the book.

 

This chapter isn't about home decoration – the measuring and cutting of strips of wallpaper, the slapping on of paste, the haphazard application to walls. No, I'll be talking about changing the image used as a background (desktop) on your computer screen.

Changing the desktop wallpaper highlights one of Java's strengths – it's platform independence, which paradoxically becomes a weakness when writing applications that interact with the OS. However, as with many criticisms of Java, Java/OS integration was a problem in the early days of the language, but a large number of solutions have appeared in recent years. I'll be utilizing JNA, which lets Java dynamically access native OS libraries. Don't confuse JNA with JNI, the Java Native Interface, a much older technology. JNI has the same aims as JNA, but is more difficult to master because of its use of C/C++ stub functions. JNA allows a programmer to code entirely in Java, employing interfaces to describe functions and structures in the native library.

I'll be focusing on Windows XP (and Windows 7), but JNA works on a range of platforms, including Linux, OSX, and Solaris.

I want my wallpaper application to change the desktop image without the user's direct intervention; it should occur whenever the machine is first switched on. This brings up another tricky aspect of Java/OS integration – how to get the OS to periodically invoke Java. My answer is to use OS scripting (i.e. a batch file), combined with OS task scheduling (i.e. a cron job). The scripting language acts as a simple interface between the OS and Java.

 

Application Overview

When the machine is switched on, the ChangeWall.bat batch file starts the GoogleWallpaper Java application. The batch file is scheduled on my Windows machine by using a freeware crontab utility called Z-Cron.

GoogleWallpaper reads in a text file of words, from words.txt. It randomly selects one of the words, and queries Google’s image search using its AJAX Search API to obtain URLs of images that match the word. The data is returned in JSON format, which is examined.

Another Web access downloads one of the images, scaling and cropping it to match the computer’s screen dimensions.

Updating the desktop image is the last step, and the only one requiring OS-specific functionality. On Windows, this involves changing its registry in three places, and requesting a desktop refresh.

 

Downloads


Navigation:


Dr. Andrew Davison
E-mail: ad@coe.psu.ac.th
Back to my home page