import java.net.*; 
import java.io.*;
import java.awt.*;
import java.util.*;
class ImagePanel extends Panel implements Observer {
 Image image;
 Movie movie;
 public void attachObservable (Movie m) {             
  if (m != movie) {                                   
   if (movie != null) {movie.deleteObserver(this);}   
   movie = m;                                         
   movie.addObserver(this);                           
   movie.changed();                                   
  }
 }
 public void setImage (String s) {
  if (s == null) {image = null;}
  else {
   // Get the image stored in the file:
   Toolkit tools = Toolkit.getDefaultToolkit();
   try{image = tools.getImage(new URL(s));}
   catch (IOException e) {}
   // Create a tracker and add the image:
   MediaTracker tracker = new MediaTracker(this);
   tracker.addImage(image, 0);
  }
  repaint();
 }
 public void update (Observable o, Object x) {         
  Movie m = (Movie) o;                                 
  setImage(m.getPoster());
 }                                                     
 public void paint(Graphics g) {
  Dimension d = size();
  if (image != null) {
   int x, y, width, height, border = 20;
   double imageRatio
    = (float) image.getHeight(this) / image.getWidth(this);
   double windowRatio = (float) d.height / d.width;
   if (imageRatio > windowRatio) {
    height = d.height - border;
    width = (int) ((float) image.getWidth(this) 
                   * (d.height - border) / image.getHeight(this));
   }
   else {
    width = d.width - border;
    height = (int) ((float) image.getHeight(this) 
                    * (d.width - border) / image.getWidth(this));
   }
   x = (d.width - width) / 2; 
   y = (d.height - height) / 2;
   g.drawImage(image, x, y, width, height, this);
  }
 }
 public Dimension minimumSize() {return new Dimension(200, 300);}
 public Dimension preferredSize() {return new Dimension(200, 300);}
}
