// package joey.art;

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.*;

public class Man extends Applet implements Runnable {

	Dimension d;
	Image man;
  	FontMetrics ftmet;
	Thread design;
	Image carpet;
	String word="ether";
	String words[] = {"head", "left shoulder", "right shoulder", "chest", "right arm", "left arm", "right hand", "left hand", "belly", "crotch", "left leg", "right leg", "right foot", "left foot"};
	int posx1=-10, posy1=-10, posx2=0, posy2=0;
	private Font font;
	int more;
	Rectangle head, lshoulder, rshoulder, chest, belly, crotch, larm, rarm, lhand, rhand2, rhand, lleg, rleg, lfoot, rfoot;
	int a=0;

	Image offscreen;
	Dimension offscreensize;
	Graphics offgraphics;
  	Color foreground, background;

	public String getAppletInfo() {
		return "Thales seriEs: carpetPoetry, by joey beRzowski";
      } // getAppletInfo


      public void start() {
		repaint();
		design.start();
      } // start


      public void stop() {
		design.stop();
      } // stop


      public void destroy() {
		design.stop();
      } // destroy


	public void init() {
		design = new Thread(this);

		d = size();
	//	if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {	
		offscreen = createImage(d.width, d.height);	
		offscreensize = d;	
		offgraphics = offscreen.getGraphics();
	//	}
		man = this.getImage(this.getDocumentBase(), this.getParameter("image"));

		foreground = getColorParameter("foreground");
       	background = getColorParameter("background");
       	if (foreground != null) this.setForeground(foreground);
       	if (background != null) this.setBackground(background);
		
		font = new Font("Courier", Font.BOLD, 16);
		ftmet = this.getFontMetrics(font);

		offgraphics.setColor(background);
		offgraphics.fillRect(0, 0, d.width, d.height);
		repaint();
	
		offgraphics.drawImage(man, 0, 0, this);

		head = new Rectangle(53,16,74,70);
			lshoulder = new Rectangle(127,73,41,53);
			rshoulder = new Rectangle(20,68,33,60);
			chest = new Rectangle(53,86,74,53);
			larm = new Rectangle(127,125,56,67);
			rarm = new Rectangle(19,127,33,63);
			lhand = new Rectangle(127,193,56,34);
			rhand = new Rectangle(20,190,33,36);
			rhand2 = new Rectangle(51,202,24,26);
			belly = new Rectangle(52,153,74,49);
			crotch = new Rectangle(52,202,74,25);
			lleg = new Rectangle(101,226,55,110);
			rleg = new Rectangle(46,226,55,110);
			lfoot = new Rectangle(101,336,55,45);
			rfoot = new Rectangle(46,336,55,45);

	} // init


	public void run() {
                
	} // run

	protected Color getColorParameter(String name) {
        String value = this.getParameter(name);
        int intvalue;
        try { intvalue = Integer.parseInt(value, 16); }
        catch (NumberFormatException e) { return null; }
        return new Color(intvalue);
    }
	public void getWord() {

		if (head.inside(posx1, posy1)) word=words[0];
			else { if (lshoulder.inside(posx1, posy1)) word=words[1];
			else { if (rshoulder.inside(posx1, posy1)) word=words[2];
			else { if (chest.inside(posx1, posy1)) word=words[3];
			else { if (rarm.inside(posx1, posy1)) word=words[4];
			else { if (larm.inside(posx1, posy1)) word=words[5];
			else { if (rhand.inside(posx1, posy1)) word=words[6];
			else { if (lhand.inside(posx1, posy1)) word=words[7];
			else { if (rhand2.inside(posx1, posy1)) word=words[7];
			else { if (belly.inside(posx1, posy1)) word=words[8];
			else { if (crotch.inside(posx1, posy1)) word=words[9];
			else { if (lleg.inside(posx1, posy1)) word=words[10];
			else { if (rleg.inside(posx1, posy1)) word=words[11];
			else { if (rfoot.inside(posx1, posy1)) word=words[12];
			else { if (lfoot.inside(posx1, posy1)) word=words[13];	
			else {
				word="ether";
			}}}}}}}}}}}}}} }

}

	public void paint(Graphics g) {
		
		g.drawImage(offscreen, 0, 0, null);

	} // paint


	public boolean mouseDown(Event e, int x, int y) {
		
		if (a==0) {
			posx1 = x; posy1 = y;
			a=1;

			getWord();

			offgraphics.setColor(background);
			offgraphics.fillRect(0, 0, d.width, d.height);
			repaint();

		}
		else
		if (a==1) {
			posx2 = x; posy2 = y;
			offgraphics.drawImage(man, 0, 0, this);
			offgraphics.setColor(foreground);
			offgraphics.setFont(font);
			int w = ftmet.stringWidth(word);
			if (posx2+w > 200) offgraphics.drawString(word, 200-w, posy2);
			else offgraphics.drawString(word, posx2, posy2);
			offgraphics.fillOval(posx1-5,posy1-5,10,10);
			repaint();

			a=0;
			
		}
	
		return true;
	}


}





