import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

public class Anacrostic extends Applet implements MouseListener, KeyListener {

   static final int BOXHEIGHT = 18;
   static final int BOXWIDTH = 22;
   static final int CLUEY= 200;
   static final int CLUEX= 230;
   static final int QUOTEW= 31;
   static final int QUOTEH= 9;

   String theQuote[] = {"*"," "," "," "," "," "," ","*"," "," "," ","*"," "," "," ",
				" "," "," "," "," "," "," ","*"," "," "," "," ","*"," "," ",
				" "," "," "," "," ","*"," "," "," "," "," "," "," "," "," ",
				"*"," "," "," "," "," "," ","*"," "," ","*"," "," "," "," ",
				" "," "," ","*"," "," "," ","*"," "," "," ","*"," "," "," ",
				" "," "," "," "," ","*"," "," ","*"," "," "," "," "," ","*",
				" "," "," "," ","*"," "," "," "," ","*"," "," "," ","*"," ",
				" "," "," "," ","*"," "," "," "," ","*"," "," ","*"," "," ",
				"*"," ","*"," "," "," "," "," ","*"," "," "," ","*"," "," ",
				" "," "," "," ","*"," "," "," "," "," "," "," ","*"," "," ",
				"*"," "," "," ","*"," "," "," ","*"," "," "," "," "," "," ",
				" "," "," ","*"," "," ","*"," "," "," "," "," "," "," "," ",
				" "," ","*"," "," "," "," "," ","*"," "," "," ","*"," "," ",
				" "," "," "," "," "," "," ","*"," "," "," ","*"," "," ","*",
				" "," "," "," "," "," "," ","*"," "," "," "," "," "," ","*",
				" "," ","*"," "," "," "," ","*"," "," "," ","*"," "," "," ",
				" "," ","*"," "," "," "," "," "," ","*"," "," "," "," ","*",
				" "," "," ","*"," "," "," "," "," "," "," ","*"," "," "," ",
				" " };

   String theAnswers[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
				  " ", " ", " ", " ", " ", " ", 
                          " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
				  " ", " ", " ", " ", " ", " ", " ",
                          " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
                          " ", " ", " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ",  
				  " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ", " ",
                          " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
                          " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
                          " ", " ", " ", " ", " ", " ", " ", " ", 
                          " ", " ", " ", " ", " ", " ", " ", " ", " ", 
                          " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ",
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", 
				  " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
   
   static final String theClues[] = {"Sweet treat",
                	                   "Merino meat",
                	                   "Classic type style: 2 wds",
                	                   "Like some turkeys",
                	                   "Hypocrite's respect: 2 wds",
                	                   "Dutch industrial center",
                	                   "Fancy boat marina: 2 wds",
                	                   "Modern: hyph wd",             	                   						 "Economist's concern",
                	                   "Emulated a fisherman",
                	                   "Ancient Celtic priest",
                	                   "Howdy Doody's pal: 2 wds",
                	                   "Testifier, sometimes",
                	                   "Brushup course",
                	                   "Make happy",
                	                   "Celebrity caricaturist",
                	                   "Song frequently heard at Word W: 2 wds",
                	                   "Preacher's word for churchgoers",
                	                   "Bay of Bengal feeder",
                	                   "Bible book after Amos",
                	                   "Epic",
                	                   "Set back",
                	                   "Maryland academy, familiarly",
                	                   "Feign",
                	                   "Craftperson's output",
                	                   "Violin virtuoso: 2 wds" };

   static final int clueLength[] = {10, 6, 10, 7, 10, 9, 10, 9, 9, 6, 5, 10, 10, 
					      9, 7, 10, 13, 8, 9, 7, 5, 6, 9, 7, 9, 13};

   // Corresponding between quote and clues
   static final int quoteToAnswers[] = {-1,214,92,160,60,20,148,-1,220,128,26,-1,222,188,56,
						    77,48,80,145,171,68,108,-1,65,99,219,119,-1,139,47,
						    122,155,22,8,130,-1,191,89,79,165,57,42,114,7,149,-1,
						    121,172,51,18,84,2,-1,40,187,-1,90,118,76,183,190,221,
						    83,-1,169,6,178,-1,104,182,36,-1,208,50,131,156,105,39,
						    146,170,-1,100,102,-1,212,180,177,67,3,-1,30,78,195,184,
						    -1,4,16,38,124,-1,173,163,46,-1,203,103,49,115,9,-1,150,
						    66,217,45,-1,28,189,-1,5,218,-1,58,-1,35,17,161,107,31,
						    -1,95,186,86,-1,144,74,206,69,210,59,-1,111,43,133,213,
						    110,81,204,-1,27,141,-1,73,96,166,-1,168,11,153,-1,194,
						    211,142,116,88,176,32,125,164,-1,98,70,-1,135,71,157,
						    200,91,101,87,120,201,193,-1,54,25,137,175,10,-1,1,199,
						    136,-1,41,14,72,94,23,123,152,138,181,-1,53,34,129,-1,
						    140,198,-1,113,205,15,63,33,0,52,-1,202,174,151,134,
						    196,85,-1,13,97,-1,216,75,209,19,-1,132,215,143,-1,93,
						    192,179,109,197,-1,29,62,147,21,117,82,-1,162,44,64,154,
						    -1,12,55,112,-1,126,185,106,158,207,61,24,-1,127,37,159,
						    167 };
   static final int answersToQuotes[] = {215,189,51,88,95,118,65,43,33,108,
						     187,156,255,225,194,212,
						     96,124,49,231,5,246,32,197,265,184,
						     10,148,115,243,90,127,165,
						     214,204,123,70,268,97,77,53,193,41,
						     141,251,113,102,29,16,106,73,48,	
						     216,203,183,256,14,40,121,138,4,264,
						     244,213,252,23,111,87,20,136,170,
						     173,195,151,134,229,58,15,91,38,
						     17,145,248,62,50,223,
						     131,178,163,37,56,
						     176,2,237,196,129,152,226,169,24,81,
						     177,82,105,68,76,261,126,21,240,144,
						     140,257,210,42,107,162,247,57,26,
						     179,46,30,198,98,166,259,
						     267,9,205,34,74,233,142,221,172,191,
						     185,200,28,207,149,161,235,133,18,78,245,6,44,
						     110,220,199,157,253,31,75,174,
						     262,269,3,125,250,101,167,39,153,
						     270,155,64,79,19,47,100,
						     219,186,164,86,66,
						     239,85,201,69,59,93,
						     260,130,54,13,116,60,36,238,181,
						     159,92,222,241,208,190,175,
						     180,218,104,146,211,135,263,72,230,
						     137,160,84,143,1,234,228,112,119,25,8,61,12 };

   int currentX;
   int currentY;
   int theLetter;

   boolean quotation = false;

   Font smFont = new Font("SansSerif", Font.BOLD, 10);
   Font medFont = new Font("SansSerif", Font.BOLD, 12);
   Font lgFont = new Font("SansSerif", Font.BOLD, 16);

   //Initialize the applet
   public void init() {
      setBackground(Color.orange);
      addMouseListener(this);
      addKeyListener(this);
   }

   public void paint(Graphics g) {
      // Paint the quotation
      g.setFont(lgFont);
      int TempX= 5;
      int TempY = 5;
	int letter = 0;
      outer: for(int j = 0; j < QUOTEH; j++) {
         for(int i = 0; i < QUOTEW; i++) {
		if(letter == 271) {
		   break outer;
		}	
            if(theQuote[letter].equals("*")) {
               g.setColor(Color.black);
               g.fillRect(TempX, TempY, BOXWIDTH, BOXHEIGHT);
            }
            else if(theQuote[letter].equals("")) {
               g.setColor(Color.white);
               g.fillRect(TempX, TempY, BOXWIDTH, BOXHEIGHT);
               g.setColor(Color.black);
	         g.drawRect(TempX, TempY, BOXWIDTH, BOXHEIGHT);
            }
            else {
               g.setColor(Color.white);
               g.fillRect(TempX, TempY, BOXWIDTH, BOXHEIGHT);
               g.setColor(Color.black);
	         g.drawRect(TempX, TempY, BOXWIDTH, BOXHEIGHT);
               g.drawString(theQuote[letter], TempX +  BOXWIDTH/3, TempY + 15);
            }
            TempX += BOXWIDTH;
		letter += 1;
         }
	   TempX = 5;
	   TempY += BOXHEIGHT;
      }

	g.setFont(medFont);
	g.drawString("The Clues", 5, CLUEY - 5);
	g.drawString("The Answers", CLUEX, CLUEY - 5);

      // Paint the clues
      TempX= 5;
      TempY = CLUEY;
      int answers = 0;
      for(int i = 0; i < theClues.length; i++) {
         g.setFont(smFont);
         g.drawString(theClues[i], TempX, TempY + 20);
         int clueX = CLUEX;
         g.setFont(lgFont);
         for(int x = 0; x < clueLength[i]; x++) {
	     g.drawString(theAnswers[answers], clueX, TempY + 19);
	     g.drawLine(clueX, TempY + 20, clueX + 15, TempY + 20);
           clueX += 20;
	     answers += 1;
         }
         TempY += 20;
      }
   }

   // The user has clicked the applet. Figure out where and what they want.
   public void mouseReleased(MouseEvent e) {
      int MEx = e.getX();
      int MEy = e.getY();

      // User pressed the quotation
      int TempX= 5;
      int TempY = 5;
      int letter = 0;
      outer:for(int j = 0; j < QUOTEH; j++) {
         for(int i = 0; i < QUOTEW; i++) {
            if(!theQuote[i].equals("*")) {
	         if((MEx > TempX) && (MEx < (TempX + BOXWIDTH)) &&
			(MEy > TempY) && (MEy < (TempY + BOXHEIGHT))) {
		      currentX = e.getX();
      		currentY = e.getY();
		      theLetter = letter;
		      quotation = true;
		      break outer;	
	 	   }
	      }
	      letter += 1;
	      TempX += BOXWIDTH;
	   }	
	   TempX = 5;
	   TempY += BOXHEIGHT;			
      }

      // User pressed one of the clues
      TempX = CLUEX - 5;
      TempY = CLUEY;
      letter = 0;
      outer: for(int i = 0; i < theClues.length; i++) {
         for(int x = 0; x < clueLength[i]; x++) {
	      if((MEx > TempX) && (MEx < (TempX + 20)) &&
		   (MEy > TempY) && (MEy < (TempY + 20))) {
	         currentX = e.getX();
      	   currentY = e.getY();
	         theLetter = letter;
	         quotation = false;
	         break outer;
	      }		
            TempX += 20;
	      letter += 1;
         }
         TempY += 20;
	   TempX = CLUEX - 5;
      }
   }

   public void mousePressed(MouseEvent e) {}

   public void mouseClicked(MouseEvent e) {}

   public void mouseEntered(MouseEvent e) {}

   public void mouseExited(MouseEvent e) {}

   public void keyPressed(KeyEvent e) {}

   public void keyTyped(KeyEvent e) {}

   public void keyReleased(KeyEvent e) {
      Graphics g = getGraphics();	
      int key = e.getKeyCode();
      int correlation = -1;
      if(quotation) {
	   correlation = quoteToAnswers[theLetter];	
         if(key > 64 && key < 91) {
	      theQuote[theLetter] = KeyEvent.getKeyText(key);
	      theAnswers[correlation] = KeyEvent.getKeyText(key);
	      if(theLetter + 1 < theQuote.length) {
	         if(!theQuote[theLetter + 1].equals("*")) {
	            theLetter += 1;
	         }
	         else {	
	            theLetter += 2;
	         }		  
	      }
	   }
         else if(key == 127) { // delete
            theQuote[theLetter] = "";
	      theAnswers[correlation] = "";
         }
         else if(key == 8) { // backspace
	      if(theLetter > 1) {
	         if(!theQuote[theLetter - 1].equals("*")) {
	            theLetter -= 1;
	         }
	         else {	
	            theLetter -= 2;
	         }
		}		  
         }
      }
      else {
 	   correlation = answersToQuotes[theLetter];	
         if(key > 64 && key < 91) {
	      theQuote[correlation] = KeyEvent.getKeyText(key);
	      theAnswers[theLetter] = KeyEvent.getKeyText(key);
	      if(theLetter + 1 < theAnswers.length) {
	         theLetter += 1;
	      }
         }
         else if(key == 127) { // delete
            theQuote[correlation] = "";
	      theAnswers[theLetter] = "";
         }  
         else if(key == 8) { // backspace
	      if(theLetter > 1) {
               theLetter -= 1;
	      }
	      else {	
	         theLetter -= 2;
	      }		  
         }
      }	
      repaint();	
   }

   public void destroy(){
      removeMouseListener(this);
      removeKeyListener(this);
   }

   public String getAppletInfo() {
      return "Anacrostic by Jay Lipp";
   }
}