FileDocCategorySizeDatePackage
PrintScribble.javaAPI DocExample1758Sat Jun 02 02:42:48 BST 2001None

PrintScribble.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.*;
import java.awt.event.*;
import java.util.*;

/** An application that can print the user's scribbles */
public class PrintScribble extends Frame {
  private short last_x = 0, last_y = 0;              // last click posistion
  private Vector lines = new Vector(256,256);        // store the scribble
  private Properties printprefs = new Properties();  // store user preferences

  public PrintScribble() {
    super("PrintScribble");

    // Add a print button.
    this.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    Button b = new Button("Print");
    this.add(b);

    // Call the print() method when the button is clicked.
    // Note anonymous class.
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) { print(); }
    });

    // Exit when the user closes the window.
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) { System.exit(0); }
    });

    // Register other event types we're interested in -- for scribbling
    enableEvents(AWTEvent.MOUSE_EVENT_MASK |
                 AWTEvent.MOUSE_MOTION_EVENT_MASK);

    // Set our initial size and pop the window up.
    this.setSize(400, 400);
    this.show();
  }

  /** Redraw (or print) the scribble based on stored lines */
  public void paint(Graphics g)
  {
    for(int i = 0; i