// Michael Tartaglia
// ColorText: Working like a typewriter with basic functionality (type, backspace, enter).


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ColorText extends Applet
implements KeyListener {
 int x = 16,			// INITIAL X COORDINATE
     yOrig = 50,		// ORIGINAL Y COORDINATE (UNCHANGING)
     y = yOrig,		// Y COORDINATE
     index = 0,		// CHARACTER INDEX
     xInc = 7,			// X INCREMENT VALUE
     yInc = 10,		// Y INCREMENT VALUE
     newLine = 16;	// NEW LINE
 boolean isBackspace = false;		// BACKSPACE USAGE BOOLEAN
 char[] oldKeys = new char[100];	// OLD LETTERS TYPED (USED FOR BACKSPACING)
 Choice colors = new Choice();	// COLORS CHOICE BOX
 TextField text = new TextField(1);	// TEXT ENTERED
 Label instruct = new Label("Choose a color then type into the box.");

 public void init()
 {
   instruct.setFont(new Font("Arial",Font.BOLD,14));
   add(instruct);

	// ADD COLOR CHOICES
   colors.addItem("Black");
   colors.addItem("Red");
   colors.addItem("Green");
   colors.addItem("Blue");
   colors.addItem("Grey");
   colors.addItem("Yellow");
   colors.addItem("Disco!");
   colors.select(0);
   colors.addKeyListener(this);
   add(colors);

	// ADD ACTION LISTENER TO TEXT BOX
   text.addKeyListener(this);
   add(text);

   setBackground(Color.white);
 }

 // KEYLISTENER METHODS
 public void keyReleased(KeyEvent k) {text.setText("");}
 public void keyPressed(KeyEvent k) {
 	// CHECK IF BACKSPACE WAS PRESSED (OR IS HELD DOWN)
   if (k.getKeyCode() == 8) isBackspace = true;
   else isBackspace = false;
 }

 public void keyTyped(KeyEvent k)
 {
   Graphics g = getGraphics();	// GET GRAPHICS PACKAGE
   g.setFont(new Font("Courier",Font.BOLD,10));
   char key = k.getKeyChar();		// GET KEY TYPED
   if (key == '\t') key = ' ';	// DISALLOWING TABSPACES
   else if (key == '\n') enter();// DISALLOW ENTRY OF "\n" ON FIELD, BUT TRIGGER NEW LINE
   else if (isBackspace) backspace(g);	// USE BACKSPACE IF ENTERED
   else type(g, key);				// ELSE SHOW CHARACTER ON SCREEN
   if (x > 350 && key == ' ') {	// AUTOMATICALLY GO TO NEW LINE AFTER X-COORDINATE > 350 PIXELS
      setDrawColor(g);
      enter();
   }
   if (y > 250) repaint();
 }

 public void type(Graphics g, char key)
 {
   if (key == ' ' && x == newLine) return;
   setDrawColor(g);					// USE PKG TO SET COLOR SELECTED
   g.drawString(""+key, x, y);	// DRAW CHARACTER "KEY"
   x += xInc;							// READY FOR NEXT CHARACTER
   oldKeys[index] = key;			// SAVE KEY AS TYPED CHARACTER
   index++;								// INCREASE INDEX
 }

 public void backspace(Graphics g)
 {
   if (index > 0) {
      --index;											// DECREASE INDEX
      if (x > newLine) x -= xInc;				// DECREASE X VALUE
      g.setColor(new Color(255,255,255));		// SET TO BACKGROUND COLOR (WHITE)
      g.drawString(""+oldKeys[index], x, y);	// RE-DRAW LAST LETTER TO BLANK OUT
   }
 }

 // RESET X AND Y TO PREPARE FOR NEW LINE
 public void enter() {x = newLine; y+= yInc; index = 0;}

 // PAINT: ensuring y starts at the right place!
 public void paint(Graphics g) {if (y > 250) y = yOrig - yInc; enter();}


 public void setDrawColor(Graphics g)
 // FUNCTION: get selected color and set applet to that color
 {
   int red = 0, green = 0, blue = 0;
   if (colors.getSelectedIndex() == 0) {
      red = green = blue = 0;
   } else if (colors.getSelectedIndex() == 1) {
      red = 255;
      green = blue = 0;
   } else if (colors.getSelectedIndex() == 2) {
      green = 180;
      red = blue = 0;
   } else if (colors.getSelectedIndex() == 3) {
      blue = 220;
      red = green = 0;
   } else if (colors.getSelectedIndex() == 4) {
      red = green = blue = 180;
   } else if (colors.getSelectedIndex() == 5) {
      red = green = 180;
      blue = 0;
   } else if (colors.getSelectedIndex() == 6) {
      red = (int)Math.floor(Math.random() * 220);
      green = (int)Math.floor(Math.random() * 220);
      blue = (int)Math.floor(Math.random() * 220);
   }
   g.setColor(new Color(red,green,blue));
 }

}
