/** Java's first GUI package was AWT. It's method of "listening" to
 events like button clicks and keyboard events was not so good,
 and some of its components like buttons were "primitive".
 
 Swing is its new GUI package. It relies on the old where it
 can. For example, the Swing class JFrame is a subclass of the
 awt class Frame. Wherever there is a class already in AWT (like
 Button), the replacement in Swing has a J in front of it (like
 JFrame). */
import java.awt.*;
import javax.swing.*;

/** Demo for 6 April 2006 lecture */
public class Demo extends JFrame{
        
    
    /** Constructor: an instance with lots of components in a JPanel
     that is placed in the Center*/
    public Demo() {
        super("Demo components");
        // The JPanel that is placed in the Center
        JPanel jpanel= new JPanel();
        Container cp= getContentPane();
        cp.add(BorderLayout.CENTER, jpanel);
        
        
        // LABELS
        JLabel jlabel= new JLabel("JLabel");
        Label label= new Label("Label");
        jpanel.add(jlabel);
        jpanel.add(label);
        
        // BUTTONS
        JButton jbutton= new JButton("JButton");
        Button button= new Button("Button");
        //jpanel.add(jbutton);
        //jpanel.add(button);
        
        // TEXT FIELDS
        JTextField jtextfield=
            new JTextField("this is a JTextField", 10);
        TextField textfield= 
            new TextField("this is a TextField", 10);
        //jpanel.add(jtextfield);
        //jpanel.add(textfield);
        
        
        // TEXT AREAS
        JTextArea jtextarea= 
        new JTextArea("this is a JTextArea", 5, 10);
        TextArea textarea= 
            new TextArea("this is a TextArea", 5, 10);
    
        JTextArea jtextarea2= 
            new JTextArea("JTextArea \nin a\nJScrollPane", 5, 10);
        
        JScrollPane jscrollpane= new JScrollPane(jtextarea2);
        //jpanel.add(jtextarea);
        //jpanel.add(textarea);
        //jpanel.add(jscrollpane);
        
        
        // RADIO BUTTONS AND CHECKBOXES
        // Three radio buttons
        JRadioButton jradiobutton1= new JRadioButton("JRadioButton 1", true);
        JRadioButton jradiobutton2= new JRadioButton("JRadioButton 2", false);
        JRadioButton jradiobutton3= new JRadioButton("JRadioButton 3", false);
        JRadioButton jradiobutton4= new JRadioButton("JRadioButton 4", false);
        ButtonGroup group= new ButtonGroup();
    
        JCheckBox jcheckbox= new JCheckBox("JCheckBox");
        
        //jpanel.add(jradiobutton1);
        //jpanel.add(jradiobutton2);
        //jpanel.add(jradiobutton3);
        //jpanel.add(jradiobutton4);
        
        //jpanel.add(jcheckbox);
        
        // All those added to group are related, in that
        // only 1 of them can be checked at any time.
        group.add(jradiobutton1);
        group.add(jradiobutton2);
        group.add(jradiobutton3);
        
        
        // COMBO BOXES AND LISTS
        String[] sarray= {"red", "blue", "green"};
        JComboBox jcombobox= new JComboBox(sarray);
    
        String[] sarray2= {"cat", "dog", "mouse", "pig"};
        JList jlist= new JList(sarray2);
    
        jpanel.add(jcombobox);
        
        jpanel.add(jlist);
        
        // JCOLORCHOOSER
        JColorChooser jc= new JColorChooser();
    
        jpanel.add(jc);
        
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocation(200,100);
        
        pack();
        setVisible(true);
    }
    
    /** Constructor: JFrame with a BorderLayout manager
     Integer i is not used */
    public Demo (int i) {
        super("Demo BorderLayout");
        
        Container cont= getContentPane();
        
        
        cont.add(BorderLayout.CENTER, new JButton("CENTER"));
        cont.add(BorderLayout.NORTH, new JButton("NORTH"));
        cont.add(BorderLayout.SOUTH, new JButton("SOUTH"));
        cont.add(BorderLayout.EAST, new JButton("EAST"));
        //cont.add(BorderLayout.WEST, new JButton("WEST"));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocation(200,100);
        
        pack();
        setVisible(true);
    }
    
    /** Constructor: JFrame with a Box in its Center and four buttons
     on the outside. The Box contains (vertically) 4 JLabels.
     The Box uses a BoxLayout Manager
     Boolean b is not used */
    public Demo (boolean b) {
        super("Demo BorderLayout");
        
        Container cont= getContentPane();
        
        Box buttonBox= new Box(BoxLayout.X_AXIS);
        buttonBox.add(new JLabel("JLabel 0"));
        buttonBox.add(new JLabel("JLabel 1"));
        buttonBox.add(new JLabel("JLabel 2"));
        buttonBox.add(new JLabel("JLabel 3"));
        
        cont.add(BorderLayout.CENTER, buttonBox);
        cont.add(BorderLayout.NORTH, new JButton("NORTH"));
        cont.add(BorderLayout.SOUTH, new JButton("SOUTH"));
        cont.add(BorderLayout.EAST, new JButton("EAST"));
        cont.add(BorderLayout.WEST, new JButton("WEST"));
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocation(200,100);
        
        pack();
        setVisible(true);
    }
    
    /** Box demo, with three buttons laid out horizontally */
    public Demo(Double j) {
        super("");
        Box b1= new Box(BoxLayout.Y_AXIS);
        b1.add(new JButton("one"));
        b1.add(new JButton("two"));
        b1.add(new JButton("three"));
        
        Container cp= getContentPane();
        cp.add(b1, BorderLayout.CENTER);
        pack();
        setResizable(false);
        setVisible(true);
    }
    
    /** Constructor: frame with title t and
     3 columns with n, n+1, and n+2 buttons. */
    public Demo(String t, int n) {
        super(t);
        
        // Create Box b1 with n buttons.
        Box b1= new Box(BoxLayout.Y_AXIS);
        for (int i= 0;  i != n;  i= i+1) {
            b1.add(new JButton("A" + i));
        }
        
        // Create Box b2 with n+1 buttons.
        Box b2= new Box(BoxLayout.Y_AXIS);
        for (int i= 0;  i != n+1;  i= i+1) {
            b2.add(new JButton("B" + i));
        }
        
        b2.add(Box.createGlue());
        b2.add(Box.createGlue());
        
        // Create Box b3 with n+3 buttons.
        Box b3= new Box(BoxLayout.Y_AXIS);
        for (int i= 0;  i != n+3;  i= i+1) {
            b3.add(new JButton("C" + i));
        }
        
        // Create horizontal box b containing b1, b2, b3
        Box b= new Box(BoxLayout.X_AXIS);
        b.add(b1);
        b.add(b2);
        b.add(b3);
        
        Container cp= getContentPane();
        cp.add(b, BorderLayout.CENTER);
        pack();
        show();
    }
    
    
        
    
    
    /** Constructor: JFrame with a Box in its Center that contains
     * a table of buttons with r rows and c columns */
    public Demo (int r, int c) {
        super("Demo BorderLayout");
        
        Container cont= getContentPane();
        Box buttonBox= new Box(BoxLayout.Y_AXIS);
        
        // create an array b[r][c] of JButtons
        Box b= new Box(BoxLayout.Y_AXIS);
        
        for (int i= 0; i != r; i= i+1) {
            Box rowBox= new Box(BoxLayout.X_AXIS);
            
            // Create c buttons and add them to rowBox
            for (int j= 0; j != c; j= j+1) {
                rowBox.add(new JButton("(" + i + ", " + j + ")"));
            }
            
            // Add rowBox to b
            b.add(rowBox);
            
        }
        
        cont.add(BorderLayout.CENTER, b);
        
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocation(200,100);
        
        pack();
        setVisible(true);
    }
    
}