//********************************************************************
// magnify.java
//********************************************************************

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

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

public class magnify extends Applet
  {
    Graphics g;

    Image offscreen_image;
    Image background;
    int width;
    int height;
    boolean done_loading_image = false;

    int box_x = 0;
    int box_y = 0;
    int box_width = 100;
    int box_height = 100;

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

    public void init()
      {
        g = getGraphics();
        
        background = getImage(getCodeBase(), "./images/YANKEE.GIF");

        offscreen_image = createImage(500, 333);
        Graphics offscreen_GC = offscreen_image.getGraphics();
        offscreen_GC.drawImage(background, 0, 0, this);
      }

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

    public boolean mouseMove(Event evt, int x, int y)
      {
        erase_box(box_x, box_y, x, y);

        box_x = x;
        box_y = y;

        if (box_x > (width - box_width/2))
          box_x = width - box_width/2;

        if (box_y > (height - box_height/2))
          box_y = height - box_width/2;

        draw_box();

        return true;
      }

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

    void draw_box()
      {
        Graphics g2;
        g2 = g.create();

        g2.clipRect(box_x, box_y, box_width, box_height);

        g2.drawImage(background, -box_x, -box_y,
                     width*2, height*2, null);

        g.setColor(Color.red);
        g.drawRect(box_x, box_y, box_width-1, box_height-1);
      }

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

    void erase_box(int old_x, int old_y, int new_x, int new_y)
      {
      Graphics g2;
      g2 = g.create();

      if (new_x <= old_x && new_y <= old_y)
        {
          g2.clipRect(new_x, new_y+box_height,
                      box_width+old_x-new_x, old_y-new_y);
          g2.drawImage(offscreen_image, 0, 0, null);
          g2 = g.create();
          g2.clipRect(new_x+box_width, new_y,
                      old_x-new_x, box_height+old_y-new_y);
          g2.drawImage(offscreen_image, 0, 0, null);
        }
      else if (new_x > old_x && new_y <= old_y)
        {
          g2.clipRect(old_x, new_y+box_height,
                      box_width+new_x-old_x, old_y-new_y);
          g2.drawImage(offscreen_image, 0, 0, null);
          g2 = g.create();
          g2.clipRect(old_x, new_y,
                      new_x-old_x, box_height+old_y-new_y);
          g2.drawImage(offscreen_image, 0, 0, null);
        }
      else if (new_x > old_x && new_y > old_y)
        {
          g2.clipRect(old_x, old_y,
                      box_width+new_x-old_x, new_y-old_y);
          g2.drawImage(offscreen_image, 0, 0, null);
          g2 = g.create();
          g2.clipRect(old_x, old_y,
                      new_x-old_x, box_height+new_y-old_y);
          g2.drawImage(offscreen_image, 0, 0, null);
        }
      else // if (new_x <= old_x && new_y > old_y)
        {
          g2.clipRect(new_x, old_y,
                      box_width+old_x-new_x, new_y-old_y);
          g2.drawImage(offscreen_image, 0, 0, null);
          g2 = g.create();
          g2.clipRect(new_x+box_width, old_y,
                      old_x-new_x, box_height+new_y-old_y);
          g2.drawImage(offscreen_image, 0, 0, null);
        }
      }

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

    public boolean imageUpdate(Image img, int infoflags, int x, int y,
                               int w, int h)
      {
        if (infoflags == ALLBITS)
          {
            width = background.getWidth(this);
            height = background.getHeight(this);

            resize(width+box_width/2, height+box_height/2);

            offscreen_image = createImage(width  + box_width /2,
                                          height + box_height/2);

            Graphics offscreen_GC = offscreen_image.getGraphics();
            offscreen_GC.setColor(Color.lightGray);
            offscreen_GC.fillRect(0, 0,
                                  width  + box_width /2,
                                  height + box_height/2);

            offscreen_GC.drawImage(background, box_width/4,
                                   box_height/4, this);

            done_loading_image = true;
            repaint();

            return false;
          }
        else
          return true;
      }

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

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

        else
          {
            showStatus("Magnify:  done");
            g.drawImage(background, box_width/4, box_height/4, this);
            draw_box();
          }
      }
  }
