Difference between revisions of "Creating a Screensaver"

From CompSciWiki
Jump to: navigation, search
(Further Reading)
m (References)
 
(44 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{Template:1010Topic
 
{{Template:1010Topic
  
|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.
+
|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 its accompanying framework, we can create a Windows screensaver right now.
  
 
|Overview=We'll extend a framework to create our own Java based screensaver.  Then, we'll give Windows access to it through another small C program.
 
|Overview=We'll extend a framework to create our own Java based screensaver.  Then, we'll give Windows access to it through another small C program.
 
}}
 
}}
  
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.
+
 
 +
 
 +
This tutorial is intended for Java programmers who have completed COMP 1010.  If this isn't you, but you're feeling adventurous, install the [http://java.sun.com/javase/downloads Java SE Development Kit] and join us for the ride!  You'll also appreciate a good text editor, such as [http://notepad-plus.sourceforge.net Notepad++].  Alternatively, you may wish to join the University of Manitoba's early computer science students and use a demo version of [http://www.textpad.com TextPad].
 +
 
 +
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==
 
==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.
+
Create [[Screensaver.java]] by pasting the code available on that page into a new file with the same name.  This will act as a framework off of which we can build our screensaver.
  
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!
+
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.  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:
 
Create [[Butterflies.java]] by either pasting the code available on that page into a new file or using the following template:
  
 
<pre>
 
<pre>
 +
import java.awt.Color;
 
import java.awt.Graphics;
 
import java.awt.Graphics;
 +
import javax.swing.JOptionPane;
  
 
public class Butterflies extends Screensaver {
 
public class Butterflies extends Screensaver {
Line 25: Line 32:
 
     // the first argument, "/c".
 
     // the first argument, "/c".
 
     if (args.length > 0 && args[0].startsWith ("/c")) {
 
     if (args.length > 0 && args[0].startsWith ("/c")) {
       // Configuration dialog code goes here.
+
       // YOUR SETTINGS DIALOG CODE GOES HERE.
 
     } else if (args.length > 0 && args[0].startsWith ("/p")) {
 
     } else if (args.length > 0 && args[0].startsWith ("/p")) {
 
       // Screensaver preview code goes here.  Windows displays previews in the
 
       // Screensaver preview code goes here.  Windows displays previews in the
       // monitor of the screensaver dialog.
+
       // monitor of the screensaver dialog.  This can safely be ignored.
 
     } else {
 
     } else {
 
       // Start the screensaver.
 
       // Start the screensaver.
Line 39: Line 46:
 
   // Draw a frame of this screensaver.
 
   // Draw a frame of this screensaver.
 
   public void drawFrame (Graphics g) {
 
   public void drawFrame (Graphics g) {
     // Frame drawing code goes here.
+
     // YOUR DRAWING CODE GOES HERE.
 
   }
 
   }
 
} // Butterflies
 
} // Butterflies
 
</pre>
 
</pre>
  
The following aspects of the above template needn't be understood:   
+
The following aspects of the above template don't need to be understood:   
 
* The "extends" keyword.
 
* The "extends" keyword.
 
* Why drawFrame isn't a static method.
 
* Why drawFrame isn't a static method.
 
* The line "new Butterflies();".
 
* The line "new Butterflies();".
 +
  
 
== Adding Custom Code ==
 
== Adding Custom Code ==
  
[[image:Butterfly.png|thumb|'''Figure 1: Butterfly Curve''' <br> The Butterfly Curve was discovered by Temple H. Fay<ref>The Butterfly Curve http://en.wikipedia.org/wiki/Butterfly_curve_(transcendental)</ref>.]]
+
Add code for drawing frames of the screensaver.  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. While the sample file demonstrates "g.setColor" and "g.fillOval", we could also use other Graphics methods, such as:
  
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.
+
* g.drawArc (int x, int y, int width, int height, int startAngle, int arcAngle)
 +
* g.drawLine (int x1, int y1, int x2, int y2)
 +
* g.drawOval (int x, int y, int width, int height)
 +
* g.drawPolygon (int[] xPoints, int[] yPoints, int nPoints)
 +
* g.drawPolyline (int[] xPoints, int[] yPoints, int nPoints)
 +
* g.drawRect (int x, int y, int width, int height)
 +
* g.drawRoundRect (int x, int y, int width, int height, int arcWidth, int arcHeight)
 +
* g.fillArc (int x, int y, int width, int height, int startAngle, int arcAngle)
 +
* g.fillOval (int x, int y, int width, int height)
 +
* g.fillPolygon (int[] xPoints, int[] yPoints, int nPoints)
 +
* g.fillRect (int x, int y, int width, int height)
 +
* g.fillRoundRect (int x, int y, int width, int height, int arcWidth, int arcHeight)
  
Curious about the drawing you can do with the parameter g of type Graphics? Consult the [http://java.sun.com/javase/7/docs/api/java/awt/Graphics.html official Java API].
+
Notice the similarity between the "draw" and "fill" methods.  In Java, graphical methods that begin with "draw" are used for drawing outlines.  "fill" methods are used to draw and fill shapes with a solid color. For more information about drawing and filling with Java's Graphics class, please visit their [http://java.sun.com/javase/7/docs/api/java/awt/Graphics.html official API reference].
  
Looking for interesting equations to use in our screensaver?  Try these links:
+
The "Screensaver.java" framework provides the following useful methods:
  
* [http://en.wikipedia.org/wiki/Butterfly_curve_(transcendental) Butterfly curve]
+
* setFrameDelay (int seconds): Sets the delay, in seconds, between frame updates of the screensaver.
 +
* getWidth(): Returns an int representing the screen's width in pixels.
 +
* getHeight(): Returns an int representing the screen's height in pixels.
 +
 
 +
Looking for interesting equations to use in our screensaver?  Try these:
 +
 
 +
[[image:Butterfly.png|thumb|'''Figure 1: Butterfly Curve''' <br> The Butterfly Curve was discovered by Temple H. Fay<ref>Fay, T. H. [http://www.jstor.org/stable/2325155 The Butterfly Curve]. The American Mathematical Monthly, 96 (5), 442-443.</ref>.]]
 +
 
 +
* [http://en.wikipedia.org/wiki/Butterfly_curve_(transcendental) Butterfly curve] as depicted in Figure 1.
 
* [http://en.wikipedia.org/wiki/Rose_(mathematics) Rose]
 
* [http://en.wikipedia.org/wiki/Rose_(mathematics) Rose]
 
* [http://jwilson.coe.uga.edu/EMAT6680Fa05/Parveen/Assignment%2010/parametric_equations.htm Wave]
 
* [http://jwilson.coe.uga.edu/EMAT6680Fa05/Parveen/Assignment%2010/parametric_equations.htm Wave]
Line 67: Line 94:
 
I recommend using a graphing program to experiment with the equations while writing our screensaver.  One such program is [http://edu.kde.org/kmplot KmPlot].  It's part of the [http://www.kde.org KDE] software compilation, which has recently been ported from Unix-like operating systems to Windows.  You can download it [http://windows.kde.org/download.php here].
 
I recommend using a graphing program to experiment with the equations while writing our screensaver.  One such program is [http://edu.kde.org/kmplot KmPlot].  It's part of the [http://www.kde.org KDE] software compilation, which has recently been ported from Unix-like operating systems to Windows.  You can download it [http://windows.kde.org/download.php 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.
+
If you choose to install KDE for KmPlot, it's simplest to select all packages when asked by the installer.  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 of the installation directory.
  
==Compile and Run==
+
The suggested drawing above is all well and good, but there's a catch.  In COMP 1010, we haven't yet learned about global variables.  In order to animate what we draw between frames, we would need global variables to keep track of object positions.  [[Butterflies.java]] gets around this by using "Math.random()", which returns a double from 0.0 to 1.0, inclusively.  If we can't remember where a drawn object was on the last frame, we can always place it randomly on this one!
  
Compile our program as usual.
+
Lastly, you may also wish to create a settings dialog for our screensaver.  While outside the scope of this tutorial, you could present a series of questions on the command line and save the user's answers in a configuration file.  When the screensaver is executed normally, its output could rely on the loaded configuration.
  
 +
The settings dialog could be substituted with a simple about message.  [[Butterflies.java]] accomplishes this with the following code:
 +
 +
<pre>
 +
final String TITLE = "The Title";
 +
final String MESSAGE = "A message.";
 +
OptionPane.showMessageDialog (null, MESSAGE, TITLE, JOptionPane.INFORMATION_MESSAGE);
 +
</pre>
 +
 +
==Compiling and Running==
 +
 +
Compile our program as usual in TextPad or from the command line.  If you use the command line, be sure to "cd" into the appropriate directory first.
 +
 +
  cd Folder\to\Butterflies
 
   javac Butterflies.java
 
   javac Butterflies.java
 +
 +
Note that "Screensaver.java" is automatically compiled with and used by "Butterflies.java".  There is no need to explicitly compile it separately.
  
 
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.
 
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.
Line 83: Line 125:
 
   java Butterflies /c
 
   java Butterflies /c
  
Compile [[Butterflies.c]] with [http://www.bloodshed.net/devcpp.html Dev-C++]Don't worry about understanding the contents of this [http://en.wikipedia.org/wiki/C_(programming_language) 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 createdRename it to "Butterflies.scr".  Verify that executing "Butterflies.scr" starts our screensaver.
+
Now that we can execute our screensaver manually, it would be nice to help automate the process for Windows to use as a regular screensaver. There's just one problemWindows expects its screensavers to have the SCR file extensionSCR files are simply EXE files with a different extension that happen to take the "/s" and "/c" arguments mentioned earlierWe need a way of getting a native Windows executable to run our Java 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!
+
To create the SCR file, start by downloading [http://www.bloodshed.net/devcpp.html Dev-C++].  Create [[Butterflies.c]] with the source code on the linked page.  Don't worry about understanding the contents of this [http://en.wikipedia.org/wiki/C_(programming_language) C program].  The important thing to remember is that it executes a Java program with the same name, excluding the file extension.  This program even passes arguments, such as "/c" and "/s", along to the Java program.  Load "Butterflies.c" in Dev-C++ and compile it by clicking the "Execute" menu and choosing "Compile".  A program named "Butterflies.exe" will be created.  Rename it to "Butterflies.scr" and ensure that it's in the same folder as our Java screensaver.  Verify that executing "Butterflies.scr" starts our screensaver.  If your main source file isn't named "Butterflies.java", be sure to rename "Butterflies.scr" accordingly.
  
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.
+
Copy all the compiled "class" files and "Butterflies.scr" into "C:\Windows\System32".  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 [http://www.java.com/en/download Java Runtime Environment] to run the screensaver, so too will your friends if you distribute it to them.
 
Just as you required a [http://www.java.com/en/download Java Runtime Environment] to run the screensaver, so too will your friends if you distribute it to them.
Line 94: Line 138:
 
* [http://java.sun.com/docs/books/tutorial/java The Java Tutorials]: Learn the remainder of Java before COMP 1020!
 
* [http://java.sun.com/docs/books/tutorial/java The Java Tutorials]: Learn the remainder of Java before COMP 1020!
 
* [http://www.amazon.ca/Programming-Language-Brian-W-Kernighan/dp/0131103628 The C Programming Language]: The most authoritative book for learning how to program in C.
 
* [http://www.amazon.ca/Programming-Language-Brian-W-Kernighan/dp/0131103628 The C Programming Language]: The most authoritative book for learning how to program in C.
 +
* [http://www.samuelpauls.com/posts/166 Butterfly Curve in C]: Get an idea of what the butterfly curve would look like if it were programmed in C.
 +
  
 
== References ==
 
== References ==
 
<references/>
 
<references/>
 +
 +
 +
<small>Copyright (C) 2009 [[User:SamuelP|Samuel Pauls]]. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".</small>

Latest revision as of 14:11, 9 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 its accompanying framework, we can create a Windows screensaver right now.

   

{{{Body}}}


This tutorial is intended for Java programmers who have completed COMP 1010. If this isn't you, but you're feeling adventurous, install the Java SE Development Kit and join us for the ride! You'll also appreciate a good text editor, such as Notepad++. Alternatively, you may wish to join the University of Manitoba's early computer science students and use a demo version of TextPad.

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. This will act as a framework off of which we can build our screensaver.

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. 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.Color;
import java.awt.Graphics;
import javax.swing.JOptionPane;

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")) {
      // YOUR SETTINGS 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.  This can safely be ignored.
    } else {
      // Start the screensaver.
      final int SECONDS = 1;
      setFrameDelay (SECONDS);
      new Butterflies();
    }
  }

  // Draw a frame of this screensaver.
  public void drawFrame (Graphics g) {
    // YOUR DRAWING CODE GOES HERE.
  }
} // Butterflies

The following aspects of the above template don't need to be understood:

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


Adding Custom Code

Add code for drawing frames of the screensaver. 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. While the sample file demonstrates "g.setColor" and "g.fillOval", we could also use other Graphics methods, such as:

  • g.drawArc (int x, int y, int width, int height, int startAngle, int arcAngle)
  • g.drawLine (int x1, int y1, int x2, int y2)
  • g.drawOval (int x, int y, int width, int height)
  • g.drawPolygon (int[] xPoints, int[] yPoints, int nPoints)
  • g.drawPolyline (int[] xPoints, int[] yPoints, int nPoints)
  • g.drawRect (int x, int y, int width, int height)
  • g.drawRoundRect (int x, int y, int width, int height, int arcWidth, int arcHeight)
  • g.fillArc (int x, int y, int width, int height, int startAngle, int arcAngle)
  • g.fillOval (int x, int y, int width, int height)
  • g.fillPolygon (int[] xPoints, int[] yPoints, int nPoints)
  • g.fillRect (int x, int y, int width, int height)
  • g.fillRoundRect (int x, int y, int width, int height, int arcWidth, int arcHeight)

Notice the similarity between the "draw" and "fill" methods. In Java, graphical methods that begin with "draw" are used for drawing outlines. "fill" methods are used to draw and fill shapes with a solid color. For more information about drawing and filling with Java's Graphics class, please visit their official API reference.

The "Screensaver.java" framework provides the following useful methods:

  • setFrameDelay (int seconds): Sets the delay, in seconds, between frame updates of the screensaver.
  • getWidth(): Returns an int representing the screen's width in pixels.
  • getHeight(): Returns an int representing the screen's height in pixels.

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

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

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 asked by the installer. 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 of the installation directory.

The suggested drawing above is all well and good, but there's a catch. In COMP 1010, we haven't yet learned about global variables. In order to animate what we draw between frames, we would need global variables to keep track of object positions. Butterflies.java gets around this by using "Math.random()", which returns a double from 0.0 to 1.0, inclusively. If we can't remember where a drawn object was on the last frame, we can always place it randomly on this one!

Lastly, you may also wish to create a settings dialog for our screensaver. While outside the scope of this tutorial, you could present a series of questions on the command line and save the user's answers in a configuration file. When the screensaver is executed normally, its output could rely on the loaded configuration.

The settings dialog could be substituted with a simple about message. Butterflies.java accomplishes this with the following code:

final String TITLE = "The Title";
final String MESSAGE = "A message.";
OptionPane.showMessageDialog (null, MESSAGE, TITLE, JOptionPane.INFORMATION_MESSAGE);

Compiling and Running

Compile our program as usual in TextPad or from the command line. If you use the command line, be sure to "cd" into the appropriate directory first.

 cd Folder\to\Butterflies
 javac Butterflies.java

Note that "Screensaver.java" is automatically compiled with and used by "Butterflies.java". There is no need to explicitly compile it separately.

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

Now that we can execute our screensaver manually, it would be nice to help automate the process for Windows to use as a regular screensaver. There's just one problem. Windows expects its screensavers to have the SCR file extension. SCR files are simply EXE files with a different extension that happen to take the "/s" and "/c" arguments mentioned earlier. We need a way of getting a native Windows executable to run our Java screensaver.

To create the SCR file, start by downloading Dev-C++. Create Butterflies.c with the source code on the linked page. Don't worry about understanding the contents of this C program. The important thing to remember is that it executes a Java program with the same name, excluding the file extension. This program even passes arguments, such as "/c" and "/s", along to the Java program. Load "Butterflies.c" in Dev-C++ and compile it by clicking the "Execute" menu and choosing "Compile". A program named "Butterflies.exe" will be created. Rename it to "Butterflies.scr" and ensure that it's in the same folder as our Java screensaver. Verify that executing "Butterflies.scr" starts our screensaver. If your main source file isn't named "Butterflies.java", be sure to rename "Butterflies.scr" accordingly.

Copy all the compiled "class" files and "Butterflies.scr" into "C:\Windows\System32". 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. Fay, T. H. The Butterfly Curve. The American Mathematical Monthly, 96 (5), 442-443.


Copyright (C) 2009 Samuel Pauls. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".