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

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

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

public class X extends Applet
  {
    Graphics g;

    Image background;
    int width = 500;
    int height = 333;

    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");
      }

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

    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(background, 25, 25, null);
          g2 = g.create();
          g2.clipRect(new_x+box_width, new_y,
                      old_x-new_x, box_height+old_y-new_y);
          g2.drawImage(background, 25, 25, 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(background, 25, 25, null);
          g2 = g.create();
          g2.clipRect(old_x, new_y,
                      new_x-old_x, box_height+old_y-new_y);
          g2.drawImage(background, 25, 25, 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(background, 25, 25, null);
          g2 = g.create();
          g2.clipRect(old_x, old_y,
                      new_x-old_x, box_height+new_y-old_y);
          g2.drawImage(background, 25, 25, 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(background, 25, 25, null);
          g2 = g.create();
          g2.clipRect(new_x+box_width, old_y,
                      old_x-new_x, box_height+new_y-old_y);
          g2.drawImage(background, 25, 25, null);
        }

        if (box_x < box_width/4)
          {
            g.setColor(Color.lightGray);
            g.fillRect(0, 0, box_width/4, height+box_height/2);
          }

        if (box_y < box_height/4)
          {
            g.setColor(Color.lightGray);
            g.fillRect(0, 0, width+box_width/2, box_height/4);
          }

        if (box_x > (width-box_width))
          {
            g.setColor(Color.lightGray);
            g.fillRect(width+box_width/4, 0,
                       box_width/4, height+box_height/2);
          }

        if (box_y > (height-box_height))
          {
            g.setColor(Color.lightGray);
            g.fillRect(0, height+box_height/4,
                       width+box_width/2, box_height/4);
          }
      }

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

    public void paint(Graphics _g)
      {
            g.drawImage(background, box_width/4, box_height/4, this);
            draw_box();
      }
  }
