import java.awt.*;
import java.awt.geom.*;
public class MeterCanvas extends Canvas {
 String title = "Title to Be Supplied";
 float minValue = 0, maxValue = 30, value = 15;
 // Paint:
 public void paint(Graphics x) {
  Graphics2D g = (Graphics2D) x; 
  // Obtain Dimension instance and draw:
  Dimension d = getSize(); 
  float meterWidth = d.width * 3 / 4;
  float dialPosition = meterWidth
   * (2 * value - maxValue - minValue) 
   / (2 * (maxValue - minValue));                               
  AffineTransform transform = new AffineTransform(); 
  transform.translate(d.width / 2, d.height / 2);       
  g.setTransform(transform);                            
  BasicStroke stroke                                            
   = new BasicStroke(10, BasicStroke.CAP_BUTT,                  
                         BasicStroke.JOIN_BEVEL);               
  g.setStroke(stroke);                                          
  GradientPaint paint                                           
   = new GradientPaint(- meterWidth / 2, 0, Color.blue,         
                       + meterWidth / 2, 0, Color.red);         
  g.setPaint(paint);                                            
  GeneralPath p = new GeneralPath();
  p.moveTo(- meterWidth / 2, -10);      
  p.lineTo(- meterWidth / 2, 0);        
  p.lineTo(meterWidth / 2, 0);          
  p.lineTo(meterWidth / 2, -10);                
  p.moveTo(dialPosition, 0);            
  p.lineTo(dialPosition, - 10);         
  g.draw(p);                            
  // Write title:
  g.setFont(new Font("Helvetica",               
                     Font.PLAIN,                
                     (int) (meterWidth / 20))); 
  FontMetrics f = g.getFontMetrics();
  int stringWidth = f.stringWidth(title);
  int stringHeight = f.getHeight();
  g.drawString(title, 
               (float)(- stringWidth / 2),
               (float)(stringHeight * 2)        
              );
 }
 // Assist in sizing:
 public Dimension getMinimumSize() {return new Dimension(150, 100);}
 public Dimension getPreferredSize() {return new Dimension(150, 100);}}
