/* TextPanel is the bit which holds text in the CurveControls

 * for the FamousCurves applet

 * Author: Ben Soares <bs@st-and.ac.uk>

 */



import java.awt.*;

import java.applet.*;

import java.util.Vector;



public class TextPanel extends Panel {



 // member variables

 int height;

 int width;

 int numberoflines;

 Vector lines;

 Color colour = Color.black;

 int fontsize = 12;

 String fontname = "Helvetica";

 int fontstyle = Font.PLAIN;

 boolean centre = true;



 // constructor methods



 public TextPanel () {



  resize(100,50);

  setBackground(Color.white);

  this.numberoflines = 1;

  this.height = size().height;

  this.width = size().width;

  this.lines = new Vector(1);

  this.colour = TextPanel.colour;

  this.repaint();



 }



 public TextPanel(String line1) {



  resize(100,50);

  setBackground(Color.white);

  this.height = size().height;

  this.width = size().width;

  this.lines = new Vector(1);

  this.lines.addElement(line1);

  this.numberoflines = 1;

  this.colour = TextPanel.colour;

  this.repaint();

 

}



 public TextPanel(Vector lines) {



  resize(100,50);

  setBackground(Color.white);

  this.height = size().height;

  this.width = size().width;

  this.lines = lines;

  this.numberoflines = lines.size();

  this.colour = TextPanel.colour;

  this.repaint();



 }



 /* end of constructor methods */



 // member methods



 public void setText(String line1) {



  String[] templines = {line1};

  this.lines = new Vector(1);

  this.lines.addElement(line1);

  this.numberoflines = 1;

  this.colour = TextPanel.colour;

  this.repaint();

 

}



 public void setText(Vector lines) {



  this.lines = lines;

  this.numberoflines = lines.size();

  this.colour = TextPanel.colour;

  this.repaint();



 }



 public void setFontSize(int f) {



  this.fontsize = f;



 } 



 public void setCentre(boolean centre) {



  this.centre = centre;



 }



 public void setColor(Color colour) {



  this.colour = colour;



 } /* end of member methods */



 // paint method 



 public void paint(Graphics g) {



  boolean textfits = false;

  Font tempfont = g.getFont();

  g.setFont(new Font(this.fontname, this.fontstyle, this.fontsize));

  FontMetrics fontmetrics = g.getFontMetrics();

  int lineheight = fontmetrics.getHeight()-1;

  g.setColor(Color.white);

  g.fillRect(0, 0, size().width-1, size().height-1);

  g.setColor(Color.black);

  g.drawRect(0, 0, size().width-1, size().height-1);

  g.setColor(this.colour);

  for (int i=0; i<=numberoflines-1; i++) {

   int lineposition = 2;

   if (this.centre) {

    lineposition = (int)((size().width - fontmetrics.stringWidth((String)(lines.elementAt(i))))/2);

   }

   g.drawString((String)(lines.elementAt(i)), lineposition, 3+(i+1)*lineheight);

  }



 } /* end of paint method */



}

