/***
Leo Robert Klein.
Copy or modify this code at your own risk.
/****/

import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.lang.Integer;

public class redScare extends java.applet.Applet {

	public static boolean showRedRect;


	Font definitionFont = new Font("Courier",Font.BOLD,12);
	Font messageFont = new Font("Times",Font.BOLD,14);
		
	int imgX = 25, imgY = 50;
	int rectX, rectY, rectWidth, rectHeight, endX, endY;
	int downX, downY, upX, upY;
	int imgWidth, imgHeight;

	Image imageArray[];
	boolean clearScreen = false;
	public static String currentMode = "thumbs";
	
	int defaultImageSet[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
	int currentImageSet[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
	int currentImage;
	
	private Button search;
	private Button thumbs;
	private Button define;
	
	public static Hashtable hashRect = new Hashtable();

	public static Vector hashVector = new Vector();

	public static Hashtable defHash_0 = new Hashtable();
	public static Hashtable defHash_1 = new Hashtable();
	public static Hashtable defHash_2 = new Hashtable();
	public static Hashtable defHash_3 = new Hashtable();
	public static Hashtable defHash_4 = new Hashtable();
	public static Hashtable defHash_5 = new Hashtable();
	public static Hashtable defHash_6 = new Hashtable();
	public static Hashtable defHash_7 = new Hashtable();
	public static Hashtable defHash_8 = new Hashtable();
	public static Hashtable defHash_9 = new Hashtable();
	public static Hashtable defHash_10 = new Hashtable();
	public static Hashtable defHash_11 = new Hashtable();
	public static Hashtable defHash_12 = new Hashtable();
	public static Hashtable defHash_13 = new Hashtable();
	public static Hashtable defHash_14 = new Hashtable();
	public static Hashtable defHash_15 = new Hashtable();

	public static Vector rectSet = new Vector();
	
	String definitionList[], gifList[], jpgList[];
	Choice definitionMenu = new Choice();
	Choice actionMenu = new Choice();


	public void init() {
		
		hashVector.addElement(defHash_0);
		hashVector.addElement(defHash_1);
		hashVector.addElement(defHash_2);
		hashVector.addElement(defHash_3);
		hashVector.addElement(defHash_4);
		hashVector.addElement(defHash_5);
		hashVector.addElement(defHash_6);
		hashVector.addElement(defHash_7);
		hashVector.addElement(defHash_8);
		hashVector.addElement(defHash_9);
		hashVector.addElement(defHash_10);
		hashVector.addElement(defHash_12);
		hashVector.addElement(defHash_13);
		hashVector.addElement(defHash_14);
		hashVector.addElement(defHash_15);

		this.setBackground(Color.white);
		
		search = new Button("search");
   		add (search);
   		
		definitionList = getStringArray("definitions");
		for ( int i = 0; i < definitionList.length; i++ ) {
			definitionMenu.addItem(definitionList[i]);
		} // for
		add(definitionMenu);

		define = new Button("define");
   		add (define);
 		thumbs = new Button("thumbs");
   		add (thumbs);
   		
		actionMenu.addItem("find all images");
		actionMenu.addItem("clear definitions");
		actionMenu.addItem("show definitions");
		add(actionMenu);

		gifList = getStringArray("gifs");
		jpgList = getStringArray("jpgs");

		imageArray = getImageArray(gifList,currentImageSet);
	
	} // init


	public String [] getStringArray (String name) {
		String param = this.getParameter(name);
		Vector stringVector = new Vector();
		if (param == null) return null;
		try {
			StringTokenizer st = new StringTokenizer(param,",");
			while (st.hasMoreElements()) {
				stringVector.addElement(st.nextToken());
			} // while
		} // try
		catch (NoSuchElementException e) {
			stringVector.addElement(param);
		} // catch
		String stringArray [] = new String [stringVector.size()];
		for (int i = 0; i < stringVector.size(); i++) {
			stringArray [i] = stringVector.elementAt(i).toString();
		} // for
		return stringArray; 
	} // getStringArray


	public Image [] getImageArray (String[] imageList, int[] currentImageSet) {
		Image imageArray [] = new Image [currentImageSet.length];
		for (int i = 0; i < currentImageSet.length; i++) {
			imageArray [i] = getImage(getDocumentBase(),imageList[currentImageSet[i]]);
		} // for								
		waitForImage(imageArray[0]);
		imgWidth = imageArray[0].getWidth(this);
		imgHeight = imageArray[0].getHeight(this);
		return imageArray; 
	} // getImageArray

	public boolean action(Event evt, Object arg) {
		
		String currentChoice = (String) arg;
		
		if (evt.target instanceof Button) {
			if (currentChoice == "search") {
				findImages(definitionMenu.getSelectedItem());
			}
			else if (currentChoice == "define") {
				if (currentMode.equals("search")) {
					currentMode = "define";
					showRedRect = false;
					repaint();
					showRedRect = true;
				}
				else if (currentMode.equals("define")) {
					setHash(definitionMenu.getSelectedItem());
				}
			}
			else if (currentChoice == "thumbs") {
				goToThumbs();
			}
		} // if button
    
		if (evt.target instanceof Choice) {
			if (currentChoice.equals("show definitions") && currentMode.equals("search")) {
				currentMode = "define";
				repaint();
				currentMode = "search";
			}
			else if (currentChoice.equals("clear definitions") && currentMode.equals("define")) {
				clearHash();
			}
			else if (currentChoice.equals("find all images")) {
				findAllImages();
			}
		} // if choice
		return true;
	} // action

	public void goToThumbs() {
		currentMode = "thumbs";
		imageArray = getImageArray(gifList,currentImageSet);
		clearScreen = true;
		rectSet.removeAllElements();
		repaint();
	}

  public boolean mouseMove (Event evt, int x, int y) {
  //	if (currentMode.equals("thumbs") || currentMode.equals("search")) {
    	checkRects (x, y);
  //  }
    return true;
  } // mouseMove

  public boolean mouseUp (Event evt, int x, int y) {
  
	if (currentMode.equals("thumbs")) {
	 	currentImage = currentImageSet[checkRects (x, y)];
		if (!(currentImage == 100)) {
			rectSet.removeAllElements();
			int tempImageSet[] = { currentImage };
			imageArray = getImageArray(jpgList,tempImageSet);
			clearScreen = true;
			repaint();
			getHash();
			currentMode = "search";
		} // if 
	} // if currentMode.equals("thumbs")
	return true;
  } // mouseMove

  public boolean mouseDown (Event evt, int x, int y) {
    downX = x;
    downY = y;
    return true;
  } // mouseDown


  public boolean mouseDrag (Event evt, int x, int y) {
    upX = x;
    upY = y;
 	if (currentMode.equals("define")) { currentRect();}
    return false;
  } // mouseDown


	public int checkRects (int x, int y) {
		Rectangle r;
		for (int i = 0; i < rectSet.size(); i++) {
			r = (Rectangle) rectSet.elementAt(i);
			if (r.inside(x,y)) {
			//	this.setCursor(new Cursor(Cursor.HAND_CURSOR));
				return i;
			} // if
		} // for
		//	this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			return 100;	
	} // checkRects


	public void setRects (int x, int y, int width, int height) {
		Rectangle currentRect = new Rectangle (x, y, width, height);
		rectSet.addElement(currentRect);
	} // setRects


	public boolean currentRect() {

		if (downX <= upX) {
			rectX = downX - imgX;
			endX  = upX - imgX;   
		}  else  {
			rectX = upX - imgX;
			endX  = downX - imgX;  
		}
 
		if (downY <= upY) {
			rectY = downY - imgY;
			endY  = upY - imgY;   
		}  else  {
			rectY = upY - imgY;
			endY  = downY - imgY;   
		}
    
		if ( rectX < 0) { rectX = 0; }
		if ( rectY < 0) { rectY = 0; }
   
		if ( endX - (rectX + imgX) > imgWidth - (rectX + imgX)) { 
			rectWidth  = (imgWidth - rectX) - 1 ; 
		} else {
			rectWidth  = (endX - rectX) - 1 ;
		}
    
		if ( endY - (rectY + imgY) > imgHeight - (rectY + imgY)) { 
			rectHeight  = (imgHeight - rectY) - 1  ; 
		} else {
			rectHeight = (endY - rectY) - 1 ;
		}


		if ((rectWidth > 0) && (rectHeight > 0)) { 
	//		message = rectX + ", " + rectY + "; " + rectWidth + ", " + rectHeight + "; ";
			repaint(); 
		}
		return true;
  } // currentRect


	public void getHash() {
		hashRect = (Hashtable) hashVector.elementAt(currentImage);
		if (! hashRect.isEmpty()) {
			int rectArray[] = new int [4];
			Enumeration hashRectElements = hashRect.elements();
			while (hashRectElements.hasMoreElements()) {
				rectArray = (int[]) hashRectElements.nextElement();
				setRects(rectArray[0] + imgX, rectArray[1] + imgY, rectArray[2], rectArray[3]);
			} // while
//		message = "Image has objects defined";
		} // if
		else {
//		message = "Image has no objects defined";
		}
	} // getHash


	public void setHash(String definitionTerm) {
		int defineRect[] = {rectX, rectY, rectWidth, rectHeight};
		hashRect.put(definitionTerm, defineRect);
		hashVector.setElementAt(hashRect, currentImage);
		repaint();
	} // setHash

	public void clearHash() {
		hashRect.clear();
		hashVector.setElementAt(hashRect, currentImage);
		showRedRect = false;
		repaint();
		showRedRect = true;
	} // clearHash


	private void waitForImage(Image image) {
		MediaTracker mt = new MediaTracker(this);
		mt.addImage(image,1);
		try {
			mt.waitForAll();
		} // try
		catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		} // catch
	} // waitForImage

	
	public void update(Graphics screen) {
	    paint (screen);  
	} // update

	protected void findImages (String definition)  {
		Vector foundSetVector = new Vector();
		Hashtable hash = new Hashtable();
		String s = "";
		for (int i = 0; i < hashVector.size(); i++) {
			hash = (Hashtable) hashVector.elementAt(i);
			if (hash.containsKey(definition)) {
				s = i + "" ;
				foundSetVector.addElement(s);
			} // if
		} // for
		if (! foundSetVector.isEmpty()) {
			int foundSetArray [] = new int [foundSetVector.size()];
			for (int i = 0; i < foundSetVector.size(); i++) {
				foundSetArray[i] = Integer.parseInt((String)foundSetVector.elementAt(i));
			} // for
		currentImageSet = foundSetArray;
		goToThumbs();

		} // if 
	
	} // findImages
	
	
		public void findAllImages ()  {
			currentImageSet = defaultImageSet;
			goToThumbs();
		} // findAllImages
	

	

	public void paint (Graphics screen) {
	
		if (clearScreen) {
			rectSet.removeAllElements();
			screen.setColor(Color.white);
			Rectangle r = getBounds();
			screen.fillRect(imgX, imgY, r.width, r.height);
			clearScreen = false;
		}
		
	if (currentMode.equals("thumbs")) {	
		thisLoop:
		for (int count = 0, picCount = 0,allPics=imageArray.length; count <= 3; count++) {
			for (int count2 = 0; count2 <= 3; count2++) {
				if ((count2+picCount>allPics)) {break thisLoop;}
				screen.drawImage (imageArray[count2 + picCount], imgX + (110 * count2), imgY + (110 * count), this);
				setRects( imgX + (110 * count2), imgY + (110 * count),100,100);
			} // for
		picCount += 4;
		} // thisLoop
	} // if thumbs


		if (currentMode.equals("search")) {	
			screen.drawImage (imageArray[0], imgX, imgY, this);
		}

		if (currentMode.equals("define")) {	
				screen.drawImage (imageArray[0], imgX, imgY, this);
		
			if (showRedRect) {
				screen.setColor(Color.red);    
				screen.drawRect( rectX + imgX, rectY + imgY, rectWidth, rectHeight);
				}
		
			if (! hashRect.isEmpty()) {
				screen.setColor(Color.blue);
				screen.setFont(definitionFont);
				String currentKey;
				int currentRectToDraw[] = new int [4];
				Enumeration hashRectKeys = hashRect.keys();
				while (hashRectKeys.hasMoreElements()) {
					currentKey = (String) hashRectKeys.nextElement();
					currentRectToDraw = (int[]) hashRect.get(currentKey);
					screen.drawRect( currentRectToDraw[0] + imgX, currentRectToDraw[1] + imgY, currentRectToDraw[2], currentRectToDraw[3]);
					screen.drawString(currentKey, currentRectToDraw[0] + imgX + 1, currentRectToDraw[1] + imgY + currentRectToDraw[3] - 1);
				} // while
			} // if
		

		} 	// if define

/***/		
		screen.setColor(Color.white);
		screen.fillRect(15, 15, 60, 30);
		screen.setColor(Color.red);
		screen.setFont(messageFont);
		screen.drawString(currentMode, 20, 40);
/****/
	

			
	} // paint

} // applet

