//********************************************************************
// eraser.java
//********************************************************************

import java.applet.*;
import java.awt.*;

//********************************************************************

public class eraser extends Applet
  {
    Graphics g;

    Image image_1;
    Image image_2;

    int width;
    int height;

    boolean done_loading_image = false;
    boolean first_time = true;

    int eraser_size = 50;

    int old_x;
    int old_y;

    //----------------------------------------------------------------

    public void init()
      {
        g = getGraphics();

        width = size().width;
        height = size().height;

        String parameter = getParameter("ERASER_SIZE");
        if (parameter != null)
          eraser_size = Integer.parseInt(parameter);

        image_1 = getImage(getCodeBase(), "image1.gif");
        image_2 = getImage(getCodeBase(), "image2.gif");

        Image offscreen_image = createImage(300, 400);
        Graphics offscreen_GC = offscreen_image.getGraphics();
        offscreen_GC.drawImage(image_1, 0, 0, this);
      }

    //----------------------------------------------------------------

    public boolean mouseMove(Event evt, int x, int y)
      {
        Graphics g2 = g.create();
        g2.setXORMode(Color.white);

        if (first_time)
          first_time = false;
        else
          g2.drawRect(old_x, old_y, eraser_size, eraser_size);

        g2.drawRect(x, y, eraser_size, eraser_size);
        old_x = x;
        old_y = y;
        return true;
      }

    //----------------------------------------------------------------

    public boolean mouseDrag(Event evt, int x, int y)
      {
        replace_image(x, y);
        return true;
      }

    //----------------------------------------------------------------

    public boolean mouseDown(Event evt, int x, int y)
      {
        replace_image(x, y);
        return true;
      }

    //----------------------------------------------------------------

    public void replace_image(int x, int y)
      {
        Graphics g2 = g.create();
        g2.setXORMode(Color.white);

        if (!first_time)
          {
            g2.drawRect(old_x, old_y, eraser_size, eraser_size);
            first_time = true;
          }

        g2.setPaintMode();
        g2.clipRect(x, y, eraser_size, eraser_size);
        g2.drawImage(image_2, 0, 0, width, height, this);
      }

    //----------------------------------------------------------------

    public boolean imageUpdate(Image img, int infoflags, int x, int y,
                               int w, int h)
      {
        if (infoflags == ALLBITS)
          {
            done_loading_image = true;
            repaint();

            return false;
          }
        else
          return true;
      }

    //----------------------------------------------------------------

    public void paint(Graphics _g)
      {
        if (!done_loading_image)
          showStatus("Eraser:  loading image");

        else
          {
            width = size().width;
            height = size().height;
            first_time = true;
            showStatus("Eraser:  done");
            g.drawImage(image_1, 0, 0, width, height, this);
          }
      }
  }
