/*
 * Thermometer.java    2.0 97/07/08
 * Copyright (c) 1997, B. F. Caviness
 *
 * This software may be copied/modified for any non-commercial educational use.
 */

 
import java.awt.*;

/**
 * The <code>class Thermometer</code> defines a canvas on which the
 * <code>paint( Graphics )</code> method draws a thermometer.
 * This code uses JDK 1.1.
 */
 
/*********************************************************/
public class Thermometer extends Canvas
{
 // Instance variables
 final int thermometerW = 20;	   // Constant for width of thermometer
 String title;
 int minTemp, maxTemp, temp;

 /****************************************/
 // Zero-parameter constructor
 Thermometer( )
 {
  title = "Title to be Supplied";
  minTemp = 0; maxTemp = 100;
  temp = 50;
 }

 /****************************************/
 public void setTitle(String t)
 {
  title = t;
  repaint();			// Repaint after setting new title
 }

 /****************************************/
 public void setTemp(int t)
 {
  if (minTemp <= t && t <= maxTemp)
   temp = t;			// Set temperature
  else
  {
   int defaultTemp = minTemp + (maxTemp - minTemp)/2;
   System.out.println("New temp out of range.  Setting to default of " +
                      defaultTemp + " degrees Fahrenheit");
   temp = defaultTemp;		// Set to default if t out of range
  }

  repaint();			// Repaint after setting new temp
 }

 /****************************************/
 public void paint(Graphics g)
 {
  Dimension d = getSize();	   // Get size of canvas
  int thermometerH = 3*(d.height - 2*thermometerW)/4; // Calculate stalk height
  
  int xOrigin = (d.width - thermometerW)/2;	// Center thermometer
  int yOrigin = (d.height - thermometerH)/2;	// on canvas

  // Calculate mercury height
  int mercuryH = thermometerH*(temp - minTemp)/(maxTemp - minTemp);

  // Fill tube w red mercury
  Color prevColor = g.getColor();	// Save current color
  g.setColor(Color.red);
  g.fillRect(xOrigin,
             yOrigin + thermometerH - mercuryH,
             thermometerW,
             mercuryH);

  // Draw outline for tube
  g.setColor(prevColor);
  g.drawRect(xOrigin, yOrigin, thermometerW, thermometerH);

  // Mark tube with temperature readings
  g.setFont(new Font("Helvetica", Font.PLAIN, 8));
  FontMetrics f = g.getFontMetrics();
  int fH = f.getHeight();
  g.drawLine(xOrigin + thermometerW + 4, yOrigin, 
             xOrigin + thermometerW + 9, yOrigin);
  g.drawString(maxTemp + " F", xOrigin + thermometerW + 13, yOrigin + fH/2); 

  g.drawLine(xOrigin + thermometerW + 4, yOrigin + thermometerH/2,
             xOrigin + thermometerW + 9, yOrigin + thermometerH/2);
  g.drawString((minTemp + maxTemp)/2 + " F", xOrigin + thermometerW + 13,
                                             yOrigin + (thermometerH + fH)/2); 

  g.drawLine(xOrigin + thermometerW + 4, yOrigin + thermometerH,
             xOrigin + thermometerW + 9, yOrigin + thermometerH);
  g.drawString(minTemp + " F", xOrigin + thermometerW + 13, 
                               yOrigin + thermometerH + fH/2); 

  // Draw bulb at bottom filled w red mercury
  g.setColor( Color.red );
  g.fillOval(xOrigin - thermometerW/2, yOrigin + thermometerH,
             2*thermometerW, 2*thermometerW);

  // Restore previous color and draw outline around bulb
  g.setColor(prevColor);		
  g.drawOval(xOrigin - thermometerW/2, yOrigin + thermometerH,
             2*thermometerW, 2*thermometerW);

  // Write title centered under thermometer
  g.setFont(new Font("Helvetica", Font.BOLD, 10));
  f = g.getFontMetrics();		// Needed to determine width of str
  g.drawString(title, (d.width - f.stringWidth(title))/2, 
               yOrigin + thermometerH + 2*thermometerW + 2*fH);
 }

 /*********************************************/
 // Inform layout managers
 public Dimension getMinimumSize(){return new Dimension(30,50);}
 public Dimension getPreferredSize(){return new Dimension(250,400);}

}
