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

/**
 * Create an applet with a text field, a password field, a textarea, 
 * radio buttons, checkboxes, buttons
 * and a menubar with submenus
 * FlowLayout manager is the default for JPanel   
 *
 * To turn a JFrame window into a JApplet applet
 * Extend from JApplet instead of JFrame
 * Change the main() method to init() method
 */

public class Awindow7 extends JApplet
{                           
    JLabel         nameLabel = new JLabel("Username: ");    //define fields
    JTextField     name      = new JTextField(15);          //to be displayed
    JLabel         pswdLabel = new JLabel("Password: ");
    JPasswordField password  = new JPasswordField(15);
    JLabel         cmntLabel = new JLabel("Comments:");
    JTextArea      comment   = new JTextArea(6, 15);

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

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

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

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

    public Awindow7()                               //constructor
    {
//      setTitle("Window with a menu Bar");         //Not available for Applet
//      setBounds(150,50,280,300);                  //Not available for Applet  
//      setDefaultCloseOperation(EXIT_ON_CLOSE);    //Not available for Applet

        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

        buildMenus();                               //call method to build menus

        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);
        rectangle.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

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

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

        pane.add(nameLabel);                        //add the components
        pane.add(name);
        pane.add(pswdLabel);
        pane.add(password);
        pane.add(cmntLabel);
        pane.add(comment);
        pane.add(line);
        pane.add(rectangle);
        pane.add(circle);
        pane.add(curve);
        pane.add(red);
        pane.add(green);
        pane.add(blue);
        pane.add(nextline);
        pane.add(enter);
        pane.add(cancel);
        
//      setResizable(false);                        //Not available for Applet
        setVisible(true);   
    }
    
   /*********************************************************************************
    * A private method to build the menu bar
    *********************************************************************************/
    private void buildMenus() {

        JMenuBar MainMenu = new JMenuBar();         //create a new menu bar         
        setJMenuBar(MainMenu);                      //set the menu bar

        //the main menu (across the menu bar) ------------------------------------

        JMenu FileMenu   = new JMenu("File");       //create 3 menus
        JMenu ColorMenu  = new JMenu("Color");
        JMenu ShapeMenu  = new JMenu("Shapes");
        MainMenu.add(FileMenu);                     //add them to menu bar
        MainMenu.add(ColorMenu);
        MainMenu.add(ShapeMenu);

        //the file menu (regular menu items) ------------------------------------

        JMenuItem optNew    = new JMenuItem("New");     //create menu items
        JMenuItem optOpen   = new JMenuItem("Open");
        JMenuItem optClose  = new JMenuItem("Close");
        JMenuItem optSave   = new JMenuItem("Save");
        JMenuItem optSaveAs = new JMenuItem("Save As...");
        JMenuItem optPrint  = new JMenuItem("Print");
        FileMenu.add(optNew);                           //add them to the file menu
        FileMenu.add(optOpen);
        FileMenu.add(optClose);
        FileMenu.addSeparator();
        FileMenu.add(optSave);
        FileMenu.add(optSaveAs);
        FileMenu.addSeparator();
        FileMenu.add(optPrint);
    
        //the color menu (checkbox menu items) ----------------------------------

        JCheckBoxMenuItem optRed   = new JCheckBoxMenuItem("Red",  false);  //create
        JCheckBoxMenuItem optGreen = new JCheckBoxMenuItem("Green",false);  //menu items
        JCheckBoxMenuItem optBlue  = new JCheckBoxMenuItem("Blue", true);   
        ColorMenu.add(optRed);                          //add them to               
        ColorMenu.add(optGreen);                        //color menu                
        ColorMenu.add(optBlue);                                         

        //the shapes menu (radio button menu items) -----------------------------

        JRadioButtonMenuItem optLine   = new JRadioButtonMenuItem("Line",true);
        JRadioButtonMenuItem optRect   = new JRadioButtonMenuItem("Rectangle",false);
        JRadioButtonMenuItem optCircle = new JRadioButtonMenuItem("Circle",false);
        JRadioButtonMenuItem optCurve  = new JRadioButtonMenuItem("Curve",false);
        ShapeMenu.add(optLine);
        ShapeMenu.add(optRect);
        ShapeMenu.add(optCircle);
        ShapeMenu.add(optCurve);

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

        shapeGrp.add(optLine);              //add the options to the group
        shapeGrp.add(optRect);
        shapeGrp.add(optCircle);
        shapeGrp.add(optCurve);
    
        //Create shortcuts ------------------------------------------------------

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

        optNew.setAccelerator  (KeyStroke.getKeyStroke('N', Event.CTRL_MASK));  //ctrl keys
        optOpen.setAccelerator (KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
        optClose.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK));
        optSave.setAccelerator (KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
        optPrint.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));
    }
}