Difference between revisions of "Screensaver.java"

From CompSciWiki
Jump to: navigation, search
(Updated the license that I'm releasing this under.)
 
Line 1: Line 1:
 +
<pre>
 
/******************************************************************************
 
/******************************************************************************
 
  * Screensaver                                                                *
 
  * Screensaver                                                                *
Line 135: Line 136:
 
   } // Frame
 
   } // Frame
 
} // Screensaver
 
} // Screensaver
 +
</pre>

Latest revision as of 09:46, 8 December 2009

/******************************************************************************
 * Screensaver                                                                *
 * Copyright (C) 2009 Samuel Pauls                                            *
 * www.samuelpauls.com                                                        *
 *                                                                            *
 * 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".                                                                  *
 *                                                                            *
 * Extend this class to make a screensaver.                                   *
 ******************************************************************************/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public abstract class Screensaver {
  private static boolean ignoreMouseMove = true;
  private static int width, height; // screen size in pixels
  private static int frameDelaySeconds = 1;

  // Create the screensaver.
  public Screensaver() {
    // Cache the screen's size.
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    width = screenSize.width;
    height = screenSize.height;
    
    new Frame();
  }
  
  // Set the delay between drawn frame's of the screensaver in seconds.
  public static void setFrameDelay (int seconds) {
    frameDelaySeconds = seconds;
  }

  // Return the screen's width in pixels.
  public static int getWidth() {
    return width;
  }

  // Return the screen's height in pixels.
  public static int getHeight() {
    return height;
  }

  // This method, which must be overridden, draws each frame of the screensaver.
  public abstract void drawFrame (Graphics g);

  // This Java Swing frame is the screensaver's window.
  class Frame extends JFrame {
    private final static int EXIT_SUCCESS = 0;
    
    // Create the Swing frame.
    public Frame() {
      // Fill the screen with the frame.
      setSize (width, height);
      setUndecorated (true); // remove the window border

      // We could draw the screensaver directly onto this Swing frame, but the
      // user would see each frame as it was being drawn.  By drawing onto a
      // panel instead, the user will only see each frame after it's drawn.
      add (new Panel());

      // Exit the screensaver upon a keystroke.
      addKeyListener (new KeyListener() {
        public void keyPressed (KeyEvent event) {
          System.exit (EXIT_SUCCESS);
        }

        public void keyReleased (KeyEvent event) {
          System.exit (EXIT_SUCCESS);
        }

        public void keyTyped (KeyEvent event) {
          System.exit (EXIT_SUCCESS);
        }
      }); // addKeyListener

      // The Swing frame is initially hidden.  Show it.
      setVisible (true);

      // Create a new thread that continuously redraws the screensaver.
      (new Thread() {
        public void run() {
          for (;;) { // forever
            // Pause before redrawing the screen.
            try {
              Thread.sleep (frameDelaySeconds * 1000);
            } catch (InterruptedException ie) {}

            repaint();
          }
        } // run
      }).start();
    }

    // The screensaver is drawn onto this panel.
    public class Panel extends JPanel {
      // Create the panel.
      public Panel() {
        // Exit the screensaver upon mouse motion.
        addMouseMotionListener (new MouseMotionListener() {
          public void mouseMoved (MouseEvent event) {
            if (ignoreMouseMove) {
              // A mouse move occurs when the screensaver starts.  Ignore it.
              ignoreMouseMove = false;
            } else {
              System.exit (EXIT_SUCCESS);
            }
          }

          public void mouseDragged (MouseEvent event) {
            System.exit (EXIT_SUCCESS);
          }
        }); // addMouseMotionListener

        // Exit the screensaver upon a mouse button click.
        addMouseListener (new MouseAdapter() {
          public void mousePressed (MouseEvent event) {
            System.exit (EXIT_SUCCESS);
          }
        }); // addMouseListener
      }

      // Called when this panel needs to be redrawn.
      public void paintComponent (Graphics g) {
        // Refer the redraw request to the client programmer's method.
        drawFrame (g);
      }
    } // Panel
  } // Frame
} // Screensaver