/* 
 * Cell.java
 *
 * by Kwan Hoi Ching Eddie
 * 
 * last revised: 06/06/1996
 *
 */

import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.util.*;

/* applet for elements in array. */
public class Cell extends Applet {

  /* set foreground and background color. */
  Color bg_color = new Color(0, 200, 200);
  Color fg_color = Color.black;
  Color highlight_color = new Color(200, 0, 200);
  Color refer_color = new Color(200, 200, 200);

  boolean selected, refered;
  int width, height;
  int xPos = 0;
  int yPos = 0;
  int type;
  String value;

  Font font;

  /* constrcutor of Cell. */
  public Cell(int w, int h, int t, Random rand) {

    font = new Font("Courier", Font.PLAIN, 12);

    width = w;
    height = h;
    type = t;
    selected = false;
    refered = false;

    switch (type) {
      /* if data type is integer. */
      case ArrayVisual.INT:
        value = (String) Integer.toString(rand.nextInt()%50);
        break;
      /* if data type is character. */
      case ArrayVisual.CHAR:
	/* 33 - 126 is the range in which character are readable. */
        int c = Math.abs(rand.nextInt());
        value = '\'' + String.valueOf((char) (c=c%94+33)) + '\'';
        break;
      /* if data type is floating point number. */
      case ArrayVisual.FLOAT:
        value = ((String) Double.toString(rand.nextDouble()*10)).substring(0,4);
        break;
      /* if data type is floating point number. */
      case ArrayVisual.DOUBLE:
        value = ((String) Double.toString(rand.nextDouble()*10)).substring(0,4);
        break;
      default:
        break;
    }
  }

  /* return the current data type. */
  public int getType() {

    return type;

  }

  /* return the value in the element. */
  public String getValue() {

    return value;

  }

  /* set the value of the element. */
  public void setValue(String str) {

    value = str;

  }

  /* set the position of the element. */
  public void setPos(int x, int y) {

    xPos = x;
    yPos = y;
  }

  /* return the x coordinates of the element. */
  public int getxPos() {

    return xPos;
  }

  /* return the y coordinates of the element. */
  public int getyPos() {

    return yPos;
  }

  /* if it is the current element referring to. */
  public void refer() {

    refered = true;

  }

  /* if it isn't the current element referring to. */
  public void unrefer() {

    refered = false;

  }

  /* if it is selected. */
  public void select() {

    selected = true;
  }

  /* if it isn't selected. */
  public void unselect() {

    selected = false;
  }

  /* paint the element on the display panel. */
  public void paint(Graphics g, int x, int y) {
  
    g.setFont(font);

    if (selected) 
      g.setColor(highlight_color);
    else
      g.setColor(bg_color);

    g.fill3DRect(x, y, width, height, true);

    if (refered) {
      g.setColor(refer_color);
      g.fill3DRect(x, y, width, height,true);
    }

    g.setColor(fg_color);
    g.drawString(value, x + width/2 - 3*value.length() , y+17);

  }

}


