import java.awt.*;
import java.util.*;
import java.awt.swing.*;
public class MeterCanvas extends Canvas implements Observer{
 Movie movie;                                           
 String title = "Title to be Supplied";
 int minValue = 0, maxValue = 30, value = 15;
 // Set the title:
 public void setTitle(String s) {title = s; repaint();}      
 // Set the current value:
 public void setValue(int v) {value = v; repaint();}
 public void attachObservable (Movie m) {
  if (m != null) {movie.deleteObserver(this);}
  movie.addObserver(this);
  movie.changed();
 }
 public void update (Observable o, Object x) {         
  setTitle(movie.getTitle());                          
  setValue(movie.rating());                            
 }                                                     
 public void paint(Graphics g) {
  super.paint(g);
  // Obtain Dimension instance and draw
  Dimension d = getSize(); 
  int meterWidth = d.width * 3 / 4;
  int dialPosition = meterWidth * (value - minValue) 
                     / (maxValue - minValue);
  int xOrigin = (d.width - meterWidth) / 2; 
  int yOrigin = d.height / 2;
  g.drawLine(xOrigin, yOrigin, xOrigin + meterWidth, yOrigin);
  g.drawLine(xOrigin + dialPosition, yOrigin,
             xOrigin + dialPosition, yOrigin - 10);
  // Write title
  g.setFont(new Font("Helvetica", Font.BOLD, 12));
  FontMetrics f = g.getFontMetrics();
  int stringWidth = f.stringWidth(title);
  int stringHeight = f.getHeight();
  xOrigin = (d.width - stringWidth) / 2; 
  yOrigin = (d.height / 2) + stringHeight * 2;                           
  g.drawString(title, xOrigin, yOrigin);
 }
 public Dimension getMinimumSize() {return new Dimension(150, 100);}
 public Dimension getPreferredSize() {return new Dimension(150, 100);}
}
