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

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

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

public class Y 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 = x;
        box_y = y;

        if (box_x > width)
          box_x = width;

        if (box_y > height)
          box_y = height;

        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_width/2, -box_y+box_height/2,
                     width*2, height*2, null);

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

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

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

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

        g2.drawImage(background, box_width/2, box_height/2, null);

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

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

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

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

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