import java.applet.Applet;
import java.awt.*;

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

public class WeatherPanel extends Panel {
  protected String city, weather;
  protected String[] weathers =
    {"rainy", "cloudy", "partly-cloudy", "sunny"};
  protected int iconWidth = 456, iconHeight=456;
  private Applet app;
  private Image[] icons = new Image[weathers.length];
  private Label cityLabel = new Label();
 
  public WeatherPanel(String city, String weather,
                      Applet app) {
    setCity(city);
    setWeather(weather);
    setApplet(app);
    for(int i=0; i<icons.length; i++)
      icons[i] = getImage(weathers[i] + ".gif");
    setLayout(new BorderLayout());
    cityLabel = new Label("Today's Weather in " +
                          city + ":",
                          Label.CENTER);
    cityLabel.setFont(new Font("TimesRoman",
                               Font.BOLD, 14));
    add("North", cityLabel);
  }

  public void paint(Graphics g) {
    g.drawImage(getIcon(), size().width-iconWidth,
                size().height-iconHeight, this);
  }

  private Image getIcon() {
    for(int i=0; i<weathers.length; i++)
      if (weathers[i].equals(weather))
        return(icons[i]);
    app.showStatus("Error: unrecognized weather '" +
                   weather + "'.");
    return(null);
  }

  private Image getImage(String file) {
    return(app.getImage(app.getDocumentBase(), file));
  }

  public void setCity(String city) {
    this.city = city;
    cityLabel.setText("Today's Weather in " +
                      city + ":");
    invalidate();
    validate();
  }

  public void setWeather(String weather) {
    this.weather = weather;
  }

  private void setApplet(Applet app) {
    this.app = app;
  }
 
}
