/* CheckBoxDemo.java        Authors: Koffman & Wolz
 * Applet for demonstrating check boxes.
 * Uses Swing and AWT.
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class CheckBoxDemo extends JApplet implements ItemListener {
   private JCheckBox temp1 = new JCheckBox("cold");
   private JCheckBox temp2 = new JCheckBox("mild");
   private JCheckBox temp3 = new JCheckBox("hot");
   private JCheckBox precip1 = new JCheckBox("dry");
   private JCheckBox precip2 = new JCheckBox("wet");
   private JCheckBox precipKind1 = new JCheckBox("rain");
   private JCheckBox precipKind2 = new JCheckBox("snow");
   private JCheckBox precipKind3 = new JCheckBox("mix");
   private JTextArea weather = new JTextArea(8, 20);

   public void init() {
     // Define the layout manager for the applet
     getContentPane().setLayout(new FlowLayout());

     // Define panel for temperature check boxes
     JPanel tempPanel = new JPanel();
     tempPanel.add(temp1);
     tempPanel.add(temp2);
     tempPanel.add(temp3);
     getContentPane().add(tempPanel);

     // Define panel for precipitation check boxes
     JPanel precipPanel = new JPanel();
     precipPanel.add(precip1);
     precipPanel.add(precip2);
     getContentPane().add(precipPanel);

     // Define panel for kind of precipitation check boxes
     JPanel precipKindPanel = new JPanel();
     precipKindPanel.add(precipKind1);
     precipKindPanel.add(precipKind2);
     precipKindPanel.add(precipKind3);
     getContentPane().add(precipKindPanel);

     // Add the output text area to the applet
     getContentPane().add(weather);

     // Register the applet as an item listener for all check boxes
     temp1.addItemListener(this);
     temp2.addItemListener(this);
     temp3.addItemListener(this);
     precip1.addItemListener(this);
     precip2.addItemListener(this);
     precipKind1.addItemListener(this);
     precipKind2.addItemListener(this);
     precipKind3.addItemListener(this);
   }

   public void itemStateChanged(ItemEvent e) {
     String weatherStr = "Here are the weather conditions:\n";
     if (temp1.isSelected())
        weatherStr += "cold\n";
     if (temp2.isSelected())
        weatherStr += "mild\n";
     if (temp3.isSelected())
        weatherStr += "hot\n";
     if (precip1.isSelected())
        weatherStr += "dry\n";
     if (precip2.isSelected())
        weatherStr += "wet\n";
     if (precipKind1.isSelected())
        weatherStr += "rain\n";
     if (precipKind2.isSelected())
        weatherStr += "snow\n";
     if (precipKind3.isSelected())
        weatherStr += "mix of snow and rain\n";
     weather.setText(weatherStr);
   }
}
