/* RadioButtonDemo.java        Authors: Koffman & Wolz
 * Applet for demonstrating radio buttons.
 * Uses Swing and AWT.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class RadioButtonDemo extends JApplet implements ActionListener {
   private JRadioButton temp1 = new JRadioButton("cold");
   private JRadioButton temp2 = new JRadioButton("mild");
   private JRadioButton temp3 = new JRadioButton("hot");
   private JRadioButton precip1 = new JRadioButton("dry");
   private JRadioButton precip2 = new JRadioButton("wet");
   private JRadioButton precipKind0 = new JRadioButton("clear");
   private JRadioButton precipKind1 = new JRadioButton("rain");
   private JRadioButton precipKind2 = new JRadioButton("snow");
   private JRadioButton precipKind3 = new JRadioButton("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 radio buttons
     JPanel tempPanel = new JPanel();
     tempPanel.add(temp1);
     tempPanel.add(temp2);
     tempPanel.add(temp3);
     getContentPane().add(tempPanel);

     // Define panel for precipitation radio buttons
     JPanel precipPanel = new JPanel();
     precipPanel.add(precip1);
     precipPanel.add(precip2);
     getContentPane().add(precipPanel);

     // Define panel for kind of precipitation radio buttons
     JPanel precipKindPanel = new JPanel();
     precipKindPanel.add(precipKind0);
     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 action listener for all radio buttons
     temp1.addActionListener(this);
     temp2.addActionListener(this);
     temp3.addActionListener(this);
     precip1.addActionListener(this);
     precip2.addActionListener(this);
     precipKind0.addActionListener(this);
     precipKind1.addActionListener(this);
     precipKind2.addActionListener(this);
     precipKind3.addActionListener(this);
 
     // Define button group for each row of radio buttons
     ButtonGroup tempGr = new ButtonGroup();
     tempGr.add(temp1);    tempGr.add(temp2);   tempGr.add(temp3);
     ButtonGroup precipGr = new ButtonGroup();
     precipGr.add(precip1);    precipGr.add(precip2);
     ButtonGroup precipKindGr = new ButtonGroup();
     precipKindGr.add(precipKind0);    precipKindGr.add(precipKind1);
     precipKindGr.add(precipKind2);    precipKindGr.add(precipKind3);
   }


   public void actionPerformed(ActionEvent e) {
     String weatherStr = "Here are the weather conditions:\n";
     if (temp1.isSelected())
        weatherStr += "cold\n";
     else if (temp2.isSelected())
        weatherStr += "mild\n";
     else if (temp3.isSelected())
        weatherStr += "hot\n";
 
     if (precip1.isSelected())
        weatherStr += "dry\n";
     else if (precip2.isSelected())
        weatherStr += "wet\n";
 
     if (precipKind0.isSelected())
        weatherStr += "no precipitation\n";
     else if (precipKind1.isSelected())
        weatherStr += "rain\n";
     else if (precipKind2.isSelected())
        weatherStr += "snow\n";
     else if (precipKind3.isSelected())
        weatherStr += "mix of snow and rain\n";
 
     weather.setText(weatherStr);
   }
}
*/
/* RadioButtonDemo.java        Authors: Koffman & Wolz
 * Applet for demonstrating radio buttons.
 * Uses Swing and AWT.
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class RadioButtonDemo extends JApplet implements ActionListener {
   private JRadioButton[] temp = {new JRadioButton("cold"),
                                  new JRadioButton("mild"),
                                  new JRadioButton("hot")};

   private JRadioButton[] precip = {new JRadioButton("dry"),
                                    new JRadioButton("wet")};

   private JRadioButton[] precipKind = {new JRadioButton("clear"),
                                        new JRadioButton("rain"),
                                        new JRadioButton("snow"),
                                        new JRadioButton("mix")};
   private JTextArea weather = new JTextArea(8, 20);

   public void init() {
     // Define the layout manager for the applet
     getContentPane().setLayout(new FlowLayout());

     // Define panel and button group for temperature radio buttons,
     //   add each button to panel and to button group and
     //   register applet as a listener for each button,
     JPanel tempPanel = new JPanel();
     ButtonGroup tempGr = new ButtonGroup();
     for (int butNum = 0; butNum < temp.length; butNum++) {
       tempPanel.add(temp[butNum]);
       tempGr.add(temp[butNum]);
       temp[butNum].addActionListener(this);
     }
     getContentPane().add(tempPanel);

     // Define panel and button group for precipitation radio buttons,
     //   add each button to panel and to button group and
     //   register applet as a listener for each button,
     JPanel precipPanel = new JPanel();
     ButtonGroup precipGr = new ButtonGroup();
     for (int butNum = 0; butNum < precip.length; butNum++) {
       precipPanel.add(precip[butNum]);
       precipGr.add(precip[butNum]);
       precip[butNum].addActionListener(this);
     }
     getContentPane().add(precipPanel);

     // Define panel and button group for precipitation kind radio buttons,
     //   add each button to panel and to button group and
     //   register applet as a listener for each button,
     JPanel precipKindPanel = new JPanel();
     ButtonGroup precipKindGr = new ButtonGroup();
     for (int butNum = 0; butNum < precipKind.length; butNum++) {
       precipKindPanel.add(precipKind[butNum]);
       precipKindGr.add(precipKind[butNum]);
       precipKind[butNum].addActionListener(this);
     }
     getContentPane().add(precipKindPanel);

     // Add the output text area to the applet
     getContentPane().add(weather);
   }


   public void actionPerformed(ActionEvent e) {
   
     String weatherStr = "Here are the weather conditions:\n";
     for (int butNum = 0; butNum < temp.length; butNum++) {
       if (temp[butNum].isSelected())
         weatherStr += (temp[butNum].getText() + "\n");
     }

     for (int butNum = 0; butNum < precip.length; butNum++) {
       if (precip[butNum].isSelected())
         weatherStr += (precip[butNum].getText() + "\n");
     }

    for (int butNum = 0; butNum < precipKind.length; butNum++) {
       if (precipKind[butNum].isSelected())
         weatherStr += (precipKind[butNum].getText() + "\n");
    }

    weather.setText(weatherStr);
   }
}

