// HashItem.java -- for Lab 10 (Hashtable)

package lab10;

import java.awt.*;

import lab07.Paintable;

/**
 * This class represents a single (key, value) pair stored in a
 * {@link Hashtable}. Both the key and value are taken to be
 * as generic as possible, namely of type <B>java.lang.Object</B>.
 */
public class HashItem implements Paintable {
    private static final Color CELL_COLOR = new Color (220,220,220);

    private Object key;
    private Object value;

    /**
     * Creates a new HashItem object storing the
     * specified (key, value) pair.
     */
    public HashItem (Object key, Object value) {
        this.key = key;
        this.value = value;
    }
  
    public Object getKey () {
        return key;
    }
  
    public Object getValue () {
        return value;
    }

    public void setValue (Object value) {
        this.value = value;
    }

    public String toString () {
        return key + ":" + value;
    }

    public void render (Graphics g, Dimension size) {
        render (g, size, new Point (size.width/2, size.height/2));
    }
    
    /**
     * Renders the HashItem object using the specified graphics context.
     * The base parameter represents the midpoint of the left-hand
     * side of the HashItem rectangle, whose dimensions are specified
     * by the size parameter.
     * @param g Graphics context.
     * @param size (width,height) of HashItem rectangle.
     * @param base (x,y) coordinate of midpoint of left-hand side
     * of Item rectangle.
     */
    public void render (Graphics g, Dimension size, Point base) {
        int width = size.width - 4;
        int height = size.height - 4;

        int xloc = base.x + 1;
        int yloc = base.y - height / 2 - 1;
        
        int x = xloc;
    
        Color oldColor = g.getColor ();
        g.setColor (CELL_COLOR);
        g.fillRect(x, yloc, width / 2, height);
        g.setColor (Color.black);
        g.drawRect(x, yloc, width / 2, height);
        g.drawString ("" + key, x + 5, base.y + 5);

        x += width / 2;
    
        g.setColor (CELL_COLOR);
        g.fillRect (x, yloc, width / 2, height);
        g.setColor (Color.black);
        g.drawRect (x, yloc, width / 2, height);
        g.drawString ("" + value, x + 5, base.y + 5);
        g.setColor (oldColor);
    }
}
