/*
    ImageApplet.java - shows silly Bill Gates face.
    Copyright (C) 1998-2002  David N. Welton <davidw@dedasys.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.
*/
import java.lang.*;
import java.awt.*;
import java.applet.Applet;

class ImageCanvas extends Canvas {
    Point point = null;
    Container pappy;
    Image image;
    boolean trueSizeKnown = false;
    Dimension minSize;
    int w, h;
    private int lastx = 0;
    private int lasty = 0;
    int radius = 20;
    int diam = radius * 2;

    public ImageCanvas(Image image, Container parent, int initialWidth, int initialHeight) {

        if (image == null) {
            System.err.println("Canvas got invalid image object!");
            return;
        }

        this.image = image;
        pappy = parent;

        w = initialWidth;
        h = initialHeight;

        minSize = new Dimension(w,h);
    }

    public Dimension preferredSize() {
        return minimumSize();
    }

    public synchronized Dimension minimumSize() {
        return minSize;
    }

    public boolean mouseMove(Event e, int x, int y) {
	Color col1 = new Color(255,255,255);
	Color col2 = new Color(0,0,0);
	int cent1x = 90;
	int cent1y = 135;
	int cent2x = 150;
	int cent2y = 135;
	double xratio1 = 0;
	double xratio2 = 0;
	double yratio1 = 0;
	double yratio2 = 0;
	double hyp1 = 0 ;
	double hyp2 = 0 ;
	int innerradius = radius - 5;

	if(point == null) {
	    point = new Point (x,y);
	} else {
	    point.x = x;
	    point.y = y;
	}
	Graphics g = getGraphics();

	/*	g.drawLine(lastx, lasty, x, y);
		lastx = x;
		lasty = y;
		*/
	g.setColor(col1);
	g.fillOval(75,120, diam, diam);
	g.fillOval(135,120, diam, diam);

	g.setColor(col2);
	g.drawOval(75,120, diam, diam);
	g.drawOval(135,120, diam, diam);

	hyp1 = Math.sqrt((x - cent1x)*(x - cent1x) + (y - cent1y)*(y - cent1y));
	hyp2 = Math.sqrt((x - cent2x)*(x - cent2x) + (y - cent2y)*(y - cent2y));
	xratio1 = (x - cent1x) / hyp1;
	xratio2 = (x - cent2x) /hyp2;
	yratio1 = (y - cent1y) /hyp1;
	yratio2 = (y - cent2y) /hyp2;

 /* 	System.out.println(xratio1);
	System.out.println(xratio2);
	System.out.println(yratio1);
	System.out.println(yratio2);
  */
 /* 	xratio1 = (x - cent1x)*(x - cent1x) / (y - cent1y)*(y - cent1y) ;
	xratio2 = (x - cent2x)*(x - cent2x) / (y - cent2y)*(y - cent2y) ;
	yratio1 = (y - cent1y)*(y - cent1y) / (x - cent1x)*(x - cent1x) ;
	yratio2 = (y - cent2y)*(y - cent2y) / (x - cent2x)*(x - cent2x) ;
  */

	if (hyp1 > innerradius) {
	    g.fillOval( (int)(xratio1 * innerradius) + cent1x, (int)(yratio1 * innerradius) + cent1y, 10, 10);
	} else {
	     g.fillOval( (x - cent1x) + cent1x, (y - cent1y) + cent1y, 10, 10);
	}

	if (hyp2 > innerradius) {
	    g.fillOval( (int)(xratio2 * innerradius) + cent2x, (int)(yratio2 * innerradius) + cent2y, 10, 10);
	} else {
	    g.fillOval( (x - cent2x) + cent2x, (y - cent2y) + cent2y, 10, 10);
	}

 /* 	g.fillOval(cent1x, cent1y, 10, 10);
	g.fillOval(cent2x, cent2y, 10, 10);
  */

 /* 	g.fillOval(1,120, diam, diam);  */

	return false;
    }

    public void paint (Graphics g) {
        if (image != null) {
            if (!trueSizeKnown) {
                int imageWidth = image.getWidth(this);
                int imageHeight = image.getHeight(this);

                if ((imageWidth > 0) && (imageHeight > 0)) {
                    trueSizeKnown = true;

                    //Component-initiated resizing.
                    w = imageWidth;
                    h = imageHeight;
                    minSize = new Dimension(w,h);
                    resize(w, h);
                    pappy.validate();
                }
            }

            g.drawImage(image, 0, 0, this);

            /*  g.drawRect(0, 0, w - 1, h - 1);  */
        }
    }
}


public class ImageApplet extends Applet {
    public void init() {
        Image image1 = getImage(getCodeBase(), "billg.jpg");

        ImageCanvas ic1 = new ImageCanvas(image1, this, 100, 100);

        add(ic1);

        validate();
    }
}

