import java.awt.*;

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

/** A Canvas (borderless window that can't hold
 *  other graphical components) that lets you
 *  specify what cursor should be used when
 *  the mouse is inside it.
 */

public class CursorCanvas extends Canvas {
  private int cursor, origCursor = Frame.DEFAULT_CURSOR;
  private Frame frame = null;

  /** Build a Canvas, recording cursor of interest. */
  
  public CursorCanvas(int width, int height,
                      int cursor, Color bgColor) {
    resize(width, height);
    setBackground(bgColor);
    this.cursor = cursor;
  }

  /** When mouse enters the Canvas, set cursor. */
  
  public boolean mouseEnter(Event event, int x, int y) {
    origCursor = getCursor();
    setCursor(cursor);
    return(false);
  }

  /** When mouse leaves, reset cursor to whatever
   *  it was when mouse entered.
   */
  
  public boolean mouseExit(Event event, int x, int y) {
    setCursor(origCursor);
    return(false);
  }

  private int getCursor() {
    if (getFrame() != null)
      return(frame.getCursorType());
    else
      return Frame.DEFAULT_CURSOR;
  }

  private void setCursor(int cursor) {
    if (getFrame() != null)
      frame.setCursor(cursor);
  }

  // Find the Frame holding the Canvas. Keep looking
  // at the enclosing window until you find a Frame.
  // This works in both applets and applications.
  
  private Frame getFrame() {
    if (frame == null) {
      Container containingWin = getParent();
      while (containingWin != null) {
        if (containingWin instanceof Frame) {
          frame = (Frame)containingWin;
          break;
        } else
          containingWin = containingWin.getParent();
      }
    }
    return(frame);
  }
}

