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

/**
 *  QuestionGUI shows us how to put an image on the screen and
 *     work with Panels to get a good layout of our elements
 * 
 * @author Kathryn McKinley
 * @version April 2007
 * 
 * 1) Show creation of two logical panels and their composition
 * 2) Show WEST, CENTER justification of the picture
 * 3) Show relative window sizing
 * 4) Show .pack to get better fit to the window
 * 5) Layout of buttons from center to left, not perfect still ;-)
 */
public class QuestionGUI implements ActionListener {
    // constants
    // text lengths
    private static final int TEXT_WIDTH    = 20;  // characters
        
    private static final String QUESTION = "\n   What is this child's name?\n";

    private static final String CRESPONSE = "   Correct! ";
    private static final String IRESPONSE = "   Guess again.";
   
    // instance variables
    // Make a window with a title - just one ever
    private JFrame window = new JFrame("Question GUI");
    
    // use the FlowLayout for the picture Panel
    private JPanel picturePanel = new JPanel(new FlowLayout());
    
    // Use a 3 x 1 layout for the question, buttons, and response
    private JPanel questionPanel = new JPanel(new GridLayout(3,1));
    
    // the question component
    private JTextArea questionArea = new JTextArea(QUESTION, 3, TEXT_WIDTH);
 
    // the response text component
    private JTextArea responseArea = new JTextArea("", 2, TEXT_WIDTH);
    
    // Put all the buttons in one line of the grid
    // Show FlowLayout vs GridLayout
    private JPanel buttonPanel = new JPanel(new GridLayout(1,3));

    // user selects one of these
    // with radio buttons, you can only select one
    private JRadioButton CooperButton  = new JRadioButton("Cooper", false);
    private JRadioButton DylanButton = new JRadioButton("Dylan", false); 
    private JRadioButton WyattButton = new JRadioButton("Wyatt", false);   
    private ButtonGroup kidButtons = new ButtonGroup();
    // find the turn on for next year!! WyattButton.toggleButtonModel();
  
    /**
     * Constructor for objects of class QuestionGUI
     *    configures the GUI
     */
    public QuestionGUI()
    {
        // register listners
        CooperButton.addActionListener(this);
        DylanButton.addActionListener(this);
        WyattButton.addActionListener(this);
        
        // read in an image into an ImageIcon
        ImageIcon picture = new ImageIcon ("child.jpg", "Picture of my Child");       
        JLabel child = new JLabel ("", picture, SwingConstants.LEFT);
        int w = picture.getIconWidth();
        int h = picture.getIconHeight();
        System.out.println("Height  Width: " + h + " " + w);

        // window size and exit 
        // window.setSize(w + (int)(.2 * w), h + (int)(.2 * h));
        window.setLayout(new BorderLayout());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        
        picturePanel.add(child);
        window.add(picturePanel, BorderLayout.EAST);   //show WEST too   
        
        // put the buttons in a collection
        kidButtons.add(CooperButton);
        kidButtons.add(DylanButton);     
        kidButtons.add(WyattButton);
            
        // Lay all the pieces out beautifully      
        questionPanel.add(questionArea);    
        buttonPanel.add(CooperButton);
        buttonPanel.add(DylanButton);   
        buttonPanel.add(WyattButton);
        questionPanel.add(buttonPanel);
        questionPanel.add(responseArea);

        // sizes the window to fit the frame       
        window.add(questionPanel, BorderLayout.SOUTH);
        window.pack();      
        // show the window after we have put in all the components
        // so the construction process is not visible to the user
        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(IRESPONSE);
        } else if (WyattButton.isSelected()) {
            responseArea.setText(CRESPONSE);
        } else {
            responseArea.setText("Please select a name");
        }
    
    }  
 
    public static void main(String[] args) {
        QuestionGUI question = new QuestionGUI();   
    }
}
