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

/**
 * Create a window with a text field, a password field, a textarea, 
 * radio buttons, checkboxes, buttons
 * and a menubar with submenus
 * Handle all associated actions  
 */

public class windowApplet extends JApplet implements ActionListener
{                           

    //Define components fields ----------------------------------------------
    
    JMenuBar MainMenu    = new JMenuBar();                  //create a new menubar         

    JMenu FileMenu       = new JMenu("File");               //create main menubar items
    JMenu ColorMenu      = new JMenu("Color");       
    JMenu ShapeMenu      = new JMenu("Shape");       

    JMenuItem fileNew    = new JMenuItem("New");            //create file submenu items
    JMenuItem fileOpen   = new JMenuItem("Open");
    JMenuItem fileClose  = new JMenuItem("Close");
    JMenuItem fileSave   = new JMenuItem("Save");
    JMenuItem fileSaveAs = new JMenuItem("Save As...");
    JMenuItem filePrint  = new JMenuItem("Print");

//  JMenuItem colorRed   = new JMenuItem("Red");            //create color submenu items
//  JMenuItem shapeLine  = new JMenuItem("Line");           //create shape submenu items

    JLabel         nameLabel = new JLabel("Username: ");    //define fields
    JTextField     name      = new JTextField(17);          //to be displayed
    JLabel         pswdLabel = new JLabel("Password: ");
    JPasswordField password  = new JPasswordField(17);
    JLabel         cmntLabel = new JLabel("Comments:");
    JTextArea      comment   = new JTextArea(6, 17);

    JCheckBox      red       = new JCheckBox("Red",   true);  
    JCheckBox      green     = new JCheckBox("Green", false);  
    JCheckBox      blue      = new JCheckBox("Blue",  false);  
    JLabel         nextline  = new JLabel("               ");    

    JRadioButton   line      = new JRadioButton("Line",      true);  
    JRadioButton   rect      = new JRadioButton("Rectangle", false);  
    JRadioButton   circle    = new JRadioButton("Circle",    false);  
    JRadioButton   curve     = new JRadioButton("Curve",     false);  

    JButton        enter     = new JButton("   ENTER   ");
    JButton        cancel    = new JButton("  CANCEL  ");
    JLabel         nextline2 = new JLabel("              ");    

    JLabel         msg1      = new JLabel("");            
    JLabel         msg2      = new JLabel("");            

    //Begin Code ----------------------------------------------------------

    public void init()                              //called by the JVM for applets
    {
        new windowApplet();                        //instantiate a program object
    }   

    public windowApplet()                          //constructor
    {
//      setTitle("Window with a Menu Bar");         //NOT available for applets
//      setBounds(150,50,280,350);                    
//      setDefaultCloseOperation(EXIT_ON_CLOSE);    

        JPanel pane = new JPanel();                 //create a JPanel
        setContentPane(pane);                       //and assign contentPane to it

        FlowLayout flow = new FlowLayout(FlowLayout.LEFT);   //force left alignment   
        pane.setLayout(flow);                                //default is center

        buildShortcuts();                           //build shortcuts for menu

        Color c = new Color(200,255,255);           //create an RGB color
        pane.setBackground(c);                      //set the background color

        red.setBackground(c);
        green.setBackground(c);
        blue.setBackground(c);
        line.setBackground(c);
        rect.setBackground(c);
        circle.setBackground(c);
        curve.setBackground(c);

        c = new Color(250,200,150);                       
        enter.setBackground(c);                     
        cancel.setBackground(c);                    
        c = new Color(50,100,150);      
        enter.setForeground(c);                
        cancel.setForeground(c);                

        Font f = new Font("Arial",Font.BOLD,12);    //PLAIN, ITALIC, BOLD
        enter.setFont(f);                           //set button font
        cancel.setFont(f);

        comment.setLineWrap(true);                  //set line wrap
        comment.setWrapStyleWord(true);             //set word wrap

        msg1.setSize(250,10);

        ButtonGroup radioGrp1 = new ButtonGroup();  //create a button group

        radioGrp1.add(line);                        //make them part of a group
        radioGrp1.add(rect);
        radioGrp1.add(circle);
        radioGrp1.add(curve);

        //Add the components to the window -----------------------------

        MainMenu.add(FileMenu);                      //items to main menubar
        MainMenu.add(ColorMenu);                     
        MainMenu.add(ShapeMenu);                     

        setJMenuBar(MainMenu);                       //set the main menubar

        FileMenu.add(fileNew);                       //add them to the file menu
        FileMenu.add(fileOpen);
        FileMenu.add(fileClose);
        FileMenu.addSeparator();
        FileMenu.add(fileSave);
        FileMenu.add(fileSaveAs);
        FileMenu.addSeparator();
        FileMenu.add(filePrint);

        pane.add(nameLabel);                        //add other components
        pane.add(name);
        pane.add(pswdLabel);
        pane.add(password);
        pane.add(cmntLabel);
        pane.add(comment);
        pane.add(red);
        pane.add(green);
        pane.add(blue);
        pane.add(nextline);
        pane.add(line);
        pane.add(rect);
        pane.add(circle);
        pane.add(curve);
        pane.add(enter);
        pane.add(cancel);
        pane.add(nextline2);
        pane.add(msg1);
        pane.add(msg2);

//      setResizable(false);                    //NOT availabler for applets
        setVisible(true);   

        fileNew.addActionListener(this);         //add an action listener on menu item
        fileOpen.addActionListener(this);             
        fileClose.addActionListener(this);             
        fileSave.addActionListener(this);             
        fileSaveAs.addActionListener(this);             
        filePrint.addActionListener(this);             

        enter.addActionListener(this);           //add an action listener on button
        cancel.addActionListener(this);            
    }
    
   /*********************************************************************************
    * A private method to build menu shortcuts
    *********************************************************************************/
    private void buildShortcuts() {
      
        //Create shortcuts ------------------------------------------------------

        FileMenu.setMnemonic('f');                      //alt keys
        ColorMenu.setMnemonic('C');
        ShapeMenu.setMnemonic('S');
        fileNew.setMnemonic('N');

        fileNew.setAccelerator  (KeyStroke.getKeyStroke('N', Event.CTRL_MASK));  //ctrl keys
        fileOpen.setAccelerator (KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
        fileClose.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK));
        fileSave.setAccelerator (KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
        filePrint.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));
    }

   /*********************************************************************************
    * Event Handlers
    *********************************************************************************/

    public void actionPerformed(ActionEvent evt) {

        Object source = evt.getSource();

        //handle menu options -----------------------------------------------------

        if (source == fileNew  || source == fileOpen   || source == fileClose ||
            source == fileSave || source == fileSaveAs || source == filePrint )
        {
            JMenuItem item     = (JMenuItem) source;                //cast down 
            String  optChosen  = item.getText();

            msg1.setText("You have chosen: " );
            msg2.setText(optChosen);            
        }

        //handle the enter button  ------------------------------------------------

        if (source == enter)
        {
            String  nameEntered  = name.getText();

            char[]  pswdChars    = password.getPassword();      //returns a char array 
            String  pswdEntered  = new String(pswdChars);       //convert to string

            String  cmntEntered  = comment.getText();       

            String  redEntered   = (red.isSelected())   ? red.getText()   : "";
            String  greenEntered = (green.isSelected()) ? green.getText() : "";
            String  blueEntered  = (blue.isSelected())  ? blue.getText()  : "";

            String  lineEntered  = (line.isSelected())   ? line.getText()   : "";
            String  rectEntered  = (rect.isSelected())   ? rect.getText()   : "";
            String  circEntered  = (circle.isSelected()) ? circle.getText() : "";
            String  curveEntered = (curve.isSelected())  ? curve.getText()  : "";

            msg1.setText("You have entered:                                        " );
            msg2.setText(nameEntered  + " " + pswdEntered  + " " + cmntEntered + " " + 
                         redEntered   + " " + greenEntered + " " + blueEntered + " " +
                         lineEntered  + " " + rectEntered  + " " + circEntered + " " + curveEntered
                        );
        }

        //handle the cancel button  -------------------------------------------------

        if (source == cancel)
        {
            name.setText("");
            password.setText("");
            comment.setText("");
            red.setSelected(false);
            green.setSelected(false);
            blue.setSelected(false);
            line.setSelected(false);
            rect.setSelected(false);
            circle.setSelected(false);
            curve.setSelected(false);
            msg1.setText("");
            msg2.setText("");
        }
    }
}