Difference between revisions of "Creating a Screensaver"

From CompSciWiki
Jump to: navigation, search
(Compile and Run)
(Compile and Run)
Line 71: Line 71:
 
==Compile and Run==
 
==Compile and Run==
  
Compile your program as usual.
+
Compile our program as usual.
  
 
   javac Butterflies.java
 
   javac Butterflies.java
  
Test your program in screensaver mode.
+
Test our program in screensaver mode.  Just as we pass the "/s" argument to the screensaver now, Windows will do the same when it attempts to start our screensaver later.
  
 
   java Butterflies /s
 
   java Butterflies /s
  
Test your program's configuration dialog.
+
Test our program's configuration dialog.  Later, Windows will tell the screensaver to show its configuration dialog with the "/c" argument.
  
 
   java Butterflies /c
 
   java Butterflies /c

Revision as of 20:43, 7 December 2009

COMP 1010 Home > Back to Chapter Topics


Introduction

Have you just completed your first Java based programming course? Are you sick of writing console based text applications? Good news! With this tutorial and it's accompanying framework, we can create a Windows screensaver right now.

   

{{{Body}}}

For the sake of this tutorial, our screensaver will be named "Butterflies." Feel free to substitute in a different name while working through this tutorial.

Setting up the Framework and Template

Create Screensaver.java by pasting the code available on that page into a new file with the same name.

At this point, you may be tempted to try to understand the "Screensaver" class. I suppose that's what makes us programmers! However, please understand that "What the heck is going on in this class?!" is the expected reaction of a programmer who has only been exposed to programming in a single course so far. As well, there are a few other things that this tutorial will ignore. Rest assured, drawing graphics isn't one of them!

Create Butterflies.java by either pasting the code available on that page into a new file or using the following template:

import java.awt.Graphics;

public class Butterflies extends Screensaver {
  public static void main (String[] args) {
    // args contains arguments passed to the program as it starts.  For example,
    // if the screensaver is run as "java Butterflies /c", args[0] will contain
    // the first argument, "/c".
    if (args.length > 0 && args[0].startsWith ("/c")) {
      // Configuration dialog code goes here.
    } else if (args.length > 0 && args[0].startsWith ("/p")) {
      // Screensaver preview code goes here.  Windows displays previews in the
      // monitor of the screensaver dialog.
    } else {
      // Start the screensaver.
      final int SECONDS = 1;
      setFrameDelay (SECONDS);
      new Butterflies();
    }
  }

  // Draw a frame of this screensaver.
  public void drawFrame (Graphics g) {
    // Frame drawing code goes here.
  }
} // Butterflies

The following aspects of the above template needn't be understood:

  • The "extends" keyword.
  • Why drawFrame isn't a static method.
  • The line "new Butterflies();".

Adding Custom Code

Figure 1: Butterfly Curve
The Butterfly Curve was discovered by Temple H. Fay[1].

Add code for drawing frames of the screensaver and it's configuration dialog. Your methods may be static even though drawFrame isn't. If you opted to use the template, I encourage you to view the sample Butterflies.java source file for an understanding of the code you could add at this point.

Curious about the drawing you can do with the parameter g of type Graphics? Consult the official Java API.

Looking for interesting equations to use in our screensaver? Try these links:

Some of the above links require that you convert from Polar to Cartesian coordinates.

I recommend using a graphing program to experiment with the equations while writing our screensaver. One such program is KmPlot. It's part of the KDE software compilation, which has recently been ported from Unix-like operating systems to Windows. You can download it here.

If you choose to install KDE for KmPlot it's simplest to select all packages when the installer asks you to make a selection. Also, while installing KDE on Windows, I found that it installed to a different directory than I had selected. In case this happens to you, be sure to pay attention to where the files are being installed to! Once installed, you can run KmPlot from the "bin" folder in the installed location.

Compile and Run

Compile our program as usual.

 javac Butterflies.java

Test our program in screensaver mode. Just as we pass the "/s" argument to the screensaver now, Windows will do the same when it attempts to start our screensaver later.

 java Butterflies /s

Test our program's configuration dialog. Later, Windows will tell the screensaver to show its configuration dialog with the "/c" argument.

 java Butterflies /c

Compile Butterflies.c with Dev-C++. Don't worry about understanding the contents of this C program. Simply create a file named "Butterflies.c", paste in its contents, and from the Execute menu in Dev-C++ click Compile. A program named "Butterflies.exe" will be created. Rename it to "Butterflies.scr". Verify that executing "Butterflies.scr" starts our screensaver.

Now move all the compiled "class" files and "Butterflies.scr" into "C:\Windows\System32". If your main source file isn't named "Butterflies.java", be sure to rename "Butterflies.scr" similarly. Our screensaver will now show up in Windows' screensaver dialog!

Congratulations you have created a screensaver using only basic Java skills. While computer science certainly has its share of complexities, there are times when asking simpler questions yields simpler answers. This screensaver could have been created in a much more complicated fashion. We skipped this needless complexity by using an existing framework and elegant curve equations found elsewhere on the Internet.

Just as you required a Java Runtime Environment to run the screensaver, so too will your friends if you distribute it to them.

Further Reading

References

  1. The Butterfly Curve http://en.wikipedia.org/wiki/Butterfly_curve_(transcendental)