import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * QuestionGUI will demonstrate how to create a pop up window where
 * users see a picture, answer a question, and get a response.
 * 
 * GUI -stands for Graphical User Interface
 * 
 * @author Kathryn McKinley
 * @version April 2007
 * 
 * 1) Show how ImageIcon works
 * 2) Change the back ground color of different elements,
 *      show JFrame and Compnent: setBackground(Color c) and Color getBackground();
 * 3) Show JFrame.remove() - e.g., remove the question
 * 4) As the last thing, change the image size and show the layout does not look
 *      pretty to motivate JPanel's for next time to group and arrange layout better
 * 
 */
public class QuestionGUI implements ActionListener {
    // constants
    // screen dimensions
    private static final int WINDOW_WIDTH  = 700;  //pixels
    private static final int WINDOW_HEIGHT = 600; //pixels
    private static final int TEXT_WIDTH    = 30; // characters
    
    // specifies how to layout the parts of the GUI
    private static final FlowLayout LAYOUT_STYLE = new FlowLayout();

    private static final String QUESTION = "   What is this child's name?";
 
    private static final String CRESPONSE = "   Correct! ";
    private static final String BRESPONSE = "   Try again.";
   
    // instance variables
    // Make a window with a title - just one ever
    private JFrame window = new JFrame("Question GUI");
 
    // user selects one of Cooper, Dylan, or Wyatt
    // with radio buttons, you can only select one
    private JRadioButton CooperButton  = new JRadioButton("Cooper");
    private JRadioButton DylanButton = new JRadioButton("Dylan"); 
    private JRadioButton WyattButton = new JRadioButton("Wyatt");   
    private ButtonGroup kidButtons = new ButtonGroup();

    // Qeustion & Response Text
    private JTextArea questionArea = new JTextArea("", 2, TEXT_WIDTH);
    private JTextArea responseArea = new JTextArea("", 2, TEXT_WIDTH);
         
    /**
     * Constructor for objects of class QuestionGUI
     *    configures the GUI
     */
    public QuestionGUI()
    {
        // register the listner
        CooperButton.addActionListener(this);
        DylanButton.addActionListener(this); 
        WyattButton.addActionListener(this);
        
        // arrange compoents in GUI
        window.setLayout(LAYOUT_STYLE);
        
        // exit program on window close
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // question area settings
        questionArea.setEditable(false);
        questionArea.setLineWrap(true);
        questionArea.setWrapStyleWord(true);
        questionArea.setText(QUESTION);
        
        // read in an image into an ImageIcon
        ImageIcon picture = new ImageIcon ("child.jpg", "Picture of my Child");       
        JLabel child = new JLabel ("", picture, SwingConstants.CENTER);
        
        // window size should depend on image size 
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                
        // put the buttons in a collection
        kidButtons.add(CooperButton);
        kidButtons.add(DylanButton);     
        kidButtons.add(WyattButton);
        
        // arrange compoents in GUI with FlowLayout       
        window.add(child);
        window.add(questionArea);
        
        window.add(CooperButton);
        window.add(DylanButton);   
        window.add(WyattButton);
        
        window.add(responseArea);
        // window.setBackground(Color.white);
       
        window.setVisible(true);
    }
    
    /**
     * event button handler code - this acts once the user selects the enter button
     * 
     * @param  user event
     */
    public void actionPerformed (ActionEvent e) 
    {
        System.out.println("We got an event!");
               
        // Use the response according to the button selection
        if (CooperButton.isSelected() || DylanButton.isSelected()) {
            responseArea.setText(BRESPONSE);
        } else if (WyattButton.isSelected()) {
            responseArea.setText(CRESPONSE);
        } else {
            responseArea.setText("Please select a name");
        }
    }
    public static void main(String[] args) {
        QuestionGUI question = new QuestionGUI();   
    }
}
