// Hiirega seotud sündmuste  käsitlemine tütaraknas.

import java.awt.*;
import java.applet.*;
/*
  <applet code="WindowEvents" width=300 height=50>
  </applet>
*/

// Create a subclass of Frame.
class SampleFrame1 extends Frame {
  String msg = "";
  int mouseX=0, mouseY=10;
  int movX=0, movY=0;

  SampleFrame1(String title) {
    super(title);
  }

  // Hide window when terminated by user.
  public boolean handleEvent(Event evtObj) {
    if(evtObj.id == Event.WINDOW_DESTROY) {
      hide();
      return true;
    }      
    return super.handleEvent(evtObj);
  }

  // Handle button press.
  public boolean mouseDown(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = x;
    mouseY = y;
    msg = "Down";
    repaint();

    return true;
  }

  // Handle button release.
  public boolean mouseUp(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = x;
    mouseY = y;
    msg = "Up";
    repaint();

    return true;
  }

  // Handle mouse move.
  public boolean mouseMove(Event evtObj, int x, int y) {
    // save coordinates
    movX = x;
    movY = y;
    repaint(0, 0, 100, 20);
    return true;
  }

  // Handle mouse drag.
  public boolean mouseDrag(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = x;
    mouseY = y;
    movX = x;
    movY = y;
    msg = "*";
    repaint();

    return true;
  }

  // Handle mouse enter.
  public boolean mouseEnter(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = 0;
    mouseY = 24;
    msg = "Mouse just entered child.";
    repaint();

    return true;
  }

  // Handle mouse exit.
  public boolean mouseExit(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = 0;
    mouseY = 24;
    msg = "Mouse just left child window.";
    repaint();

    return true;
  }

  public void paint(Graphics g) {
    g.drawString(msg, mouseX, mouseY);
    g.drawString("Mouse at " + movX + ", " + movY, 0, 10);
  }    
}

// Applet window.
public class WindowEvents extends Applet{
  SampleFrame1 f;
  String msg = "";
  int mouseX=0, mouseY=10;
  int movX=0, movY=0;
  
  // Create a frame window.
  public void init() {
    f = new SampleFrame1("Handle Mouse Events");
    f.show();
    f.resize(300, 200);
  }

  // Remove frame window when stopping applet.
  public void stop() {
    f.hide(); 
  }

  // Show frame window when starting applet.
  public void start() {
    f.show();
  }

  // Handle button press.
  public boolean mouseDown(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = x;
    mouseY = y;
    msg = "Down";
    repaint();

    return true;
  }

  // Handle button release.
  public boolean mouseUp(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = x;
    mouseY = y;
    msg = "Up";
    repaint();

    return true;
  }

  // Handle mouse move.
  public boolean mouseMove(Event evtObj, int x, int y) {
    // save coordinates
    movX = x;
    movY = y;
    repaint(0, 0, 100, 20);
    return true;
  }

  // Handle mouse drag.
  public boolean mouseDrag(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = x;
    mouseY = y;
    movX = x;
    movY = y;
    msg = "*";
    repaint();

    return true;
  }

  // Handle mouse enter.
  public boolean mouseEnter(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = 0;
    mouseY = 24;
    msg = "Mouse just entered applet window.";
    repaint();

    return true;
  }

  // Handle mouse exit.
  public boolean mouseExit(Event evtObj, int x, int y) {
    // save coordinates
    mouseX = 0;
    mouseY = 24;
    msg = "Mouse just left applet window.";
    repaint();

    return true;
  }

  // Display msg in applet window.
  public void paint(Graphics g) {
    g.drawString(msg, mouseX, mouseY);
    g.drawString("Mouse at " + movX + ", " + movY, 0, 10);
 }    
}