/* MovableImage.java 
   20/08/99
   Movable image class
   */
   
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.*;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.MalformedURLException;

/* The base movable image class 
   */
class MovableImage {
   /* The parent applet */
   MoveImage parent;
   /* The Image */
   Image movimg;
   /* The X location of the image */
   int X;
   /* The Y location of the image */
   int Y;
   /* The width of the image */
   int W;
   /* The height of the image */
   int H;
   /* flag to indicate wether the image is being dragged */
   boolean beingdragged = false;
   /* The maximum movable area of the image */
   int MaxX, MaxY;
   
   /* Get the name of the image and the position */
   public void init(MoveImage parent, Image img, int w, int h, String args, int maxx, int maxy) {
      this.parent = parent;
      movimg = img;
      StringTokenizer st = new StringTokenizer(args, ",");
      X = Integer.parseInt(st.nextToken());
      Y = Integer.parseInt(st.nextToken());
      W = w;
      H = h;
      MaxX = maxx - W;
      MaxY = maxy - H;
      //System.out.println("dimentions: "+W+","+H+" at: "+X+","+Y);
      }
   
   public void repaint() {
      int x,y,w,h;
      x = (lastX < X) ? lastX : X;
      y = (lastY < Y) ? lastY : Y;
      w = W + ((x == lastX) ? (X - lastX) : (lastX - X));
      h = H + ((y == lastY) ? (Y - lastY) : (lastY - Y));      
      parent.repaint(0,x,y,w,h);
      }
     
   public boolean inside(int x, int y) {
      return ( x >= X && x < (X + W) && y >= Y && y < (Y + H) );
      }
      
   public void drawimg(Graphics g) {
      g.drawImage(movimg, X, Y, parent);
      }
   
   int relX = 0;
   int relY = 0;
   int lastX = X;
   int lastY = Y;
         
   public void press(int x, int y) {
      if (inside(x,y)) {
         //System.out.println("inside: "+x+","+y);
         beingdragged = true;
         relX = x - X;
         relY = y - Y;
         //System.out.println("rel: "+relX+","+relY);
         repaint();
         }
      }
      
   public void dragged(int x, int y) {
      if (beingdragged) {
         lastX = X;
         lastY = Y;
         X = x - relX;
         Y = y - relY;
         X = (X > MaxX) ? MaxX : X;
         Y = (Y > MaxY) ? MaxY : Y;
         X = (X < 0) ? 0 : X;
         Y = (Y < 0) ? 0 : Y;
         repaint();
         }
      }
      
   public void lift(int x, int y) {
      if (beingdragged) {
         X = x - relX;
         Y = y - relY;         
         X = (X > MaxX) ? MaxX : X;
         Y = (Y > MaxY) ? MaxY : Y;
         X = (X < 0) ? 0 : X;
         Y = (Y < 0) ? 0 : Y;
         beingdragged = false;
         repaint();
         }
      }
      
   }   
