FileDocCategorySizeDatePackage
SerializedScribble.javaAPI DocExample4204Sat Jun 02 02:43:40 BST 2001None

SerializedScribble.java

// This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com)
// Copyright (c) 1997 by David Flanagan
// This example is provided WITHOUT ANY WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for non-commercial purposes.
// For any commercial use, see http://www.davidflanagan.com/javaexamples

import java.awt.*;               // ScrollPane, PopupMenu, MenuShortcut, etc.
import java.awt.event.*;         // New event model.
import java.io.*;                // Object serialization streams.
import java.util.zip.*;          // Data compression/decompression streams.
import java.util.Vector;         // To store the scribble in.

/**
 * This class demonstrates the use of object serialization to provide
 * a file format for saving application state.  It saves a user's scribbles
 * as a compressed, serialized Vector of Line objects.
 **/
public class SerializedScribble extends Frame {
  /** A very simple main() method for our program. */
  public static void main(String[] args) { new SerializedScribble(); }

  /** Create a Frame, Menu, and Scribble component */
  public SerializedScribble() {
    super("SerialziedScribble");             // Create the window.

    final Scribble scribble;
    scribble = new Scribble(this, 300, 300); // Create a bigger scribble area.
    this.add(scribble, "Center");            // Add it to the ScrollPane.

    MenuBar menubar = new MenuBar();         // Create a menubar.
    this.setMenuBar(menubar);                // Add it to the frame.
    Menu file = new Menu("File");            // Create a File menu.
    menubar.add(file);                       // Add to menubar.

    // Create three menu items, with menu shortcuts, and add to the menu.
    MenuItem load, save, quit;
    file.add(load = new MenuItem("Load"));
    file.add(save = new MenuItem("Save"));
    file.addSeparator();                     // Put a separator in the menu
    file.add(quit = new MenuItem("Quit"));

    // Create and register action listener objects for the three menu items.
    load.addActionListener(new ActionListener() {     // Open a new window
      public void actionPerformed(ActionEvent e) { scribble.load(); }
    });
    save.addActionListener(new ActionListener() {     // Close this window.
      public void actionPerformed(ActionEvent e) { scribble.save(); }
    });
    quit.addActionListener(new ActionListener() {     // Quit the program.
      public void actionPerformed(ActionEvent e) { System.exit(0); }
    });

    // Another event listener, this one to handle window close requests.
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });

    // Set the window size and pop it up.
    this.pack();
    this.show();
  }

  /**
   * This class is a custom component that supports scribbling.  Note that 
   * it extends Component rather than Canvas, making it "lightweight."
   **/
  static class Scribble extends Component {
    protected short last_x, last_y;               // Coordinates of last click.
    protected Vector lines = new Vector(256,256); // Store the scribbles.
    protected int width, height;                  // The preferred size.
    protected Frame frame;                        // The frame we are within.
    
    /** This constructor requires a Frame and a desired size */
    public Scribble(Frame frame, int width, int height) {
      this.frame = frame;
      this.width = width;
      this.height = height;
      
      // We handle scribbling with low-level events, so we must specify
      // which events we are interested in.
      this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
      this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
    }
    
    /** 
     * Specifies big the component would like to be.  It always returns the
     * preferred size passed to the Scribble() constructor 
     **/
    public Dimension getPreferredSize() {return new Dimension(width, height);}
    
    /** Draw all the saved lines of the scribble */
    public void paint(Graphics g) {
      for(int i = 0; i