/*----------------------------------------------------------------------+
|       Title:  GeneomePanel.java                                       |
|               Java Class                                              |
|               (used in Phylap.java applet)                            |
|                                                                       |
|       Author: David E. Joyce                                          |
|               Department of Mathematics and Computer Science          |
|               Clark University                                        |
|               Worcester, MA 01610-1477                                |
|               U.S.A.                                                  |
|                                                                       |
|               http://aleph0.clarku.edu/~djoyce/                       |
|                                                                       |
|       Date:   November, 2002.                                         |
+----------------------------------------------------------------------*/

import java.awt.*;
import java.lang.*;

public class GenomePanel extends Panel {

  private int genome[][];
  int species;
  int alt;

  public GenomePanel () {
    setFont(new Font("TimesRoman",Font.PLAIN,10));
  }

  public void setGenome (int[][] genomeVal, int speciesVal, int altVal) {
    genome = genomeVal;
    species = speciesVal;
    alt = altVal;
  }

  private Color chooseColor(int i, int alt) {
    return new Color( Color.HSBtoRGB
               ((float)((i+0.0)/alt),	// hue
			    1.0f,           // sat
			    1.0f));          // bright
  }

  private void drawNumber(Graphics g, int i, int x, int y, Color bg) {
    String digits = Integer.toString(i);
    Color curColor = g.getColor();
    g.setColor(bg);
    FontMetrics fm = g.getFontMetrics();
    int w = fm.stringWidth(digits)+10;
    int h = fm.getHeight()+4;
    g.fillRoundRect(x-w/2+1,y-h/2, w-3,h-1, 6,5);
    g.setColor(curColor);
    g.drawRoundRect(x-w/2+1,y-h/2, w-3,h-1, 6,5);
    g.drawString(digits, x-(w-10)/2, y-(h-4)/2 + fm.getAscent());
  } // drawNumber

  public void update(Graphics g) {
    if (genome == null) return; // not yet initialized
    // Draw the graph
    Dimension d = getSize();
    g.setColor(Color.white);
    g.fillRect (0,0,d.width,d.height);
    int left = 0, right;
    for (int i=0; i<species; ++i) {
      int low = 0, high;
      right = (i+1)*d.width/species;
      for (int j=0; j<genome[0].length; ++j) {
        g.setColor(chooseColor(genome[i][j],alt));
        high = (j+1)*(d.height-20)/genome[0].length;
        g.fillRect (left+1,low,right-left-2,high-low);
        low = high;
      } // for
      g.setColor(Color.black);
      drawNumber(g, i+1, (left+right)/2,d.height-10, Color.white);
      left = right;
    } // for i
  } // update

  public void paint(Graphics g) {
    update(g);
  }
   
} //Phyltree



