Complete Applet

We can now put all the pieces together. Below is the complete program. Following that, you will find an anchor you can use to run the applet.


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class step4 extends Applet implements ActionListener, AdjustmentListener  {

// adds scrollbars and message display area

MyFrame outerBox;

Panel centerPanel;
Panel bottomPanel;

  public void init ( ){
    buildWindow ();
    drawSomething ();
  } // end init

  //****************** buildWindow

    // *** Global variables for ui components

    MenuBar menuBar;

    Menu fileMenu;
      MenuItem fileNew, fileOpen, fileClose, fileSave, fileQuit;

    Menu editMenu;
      MenuItem editCut, editCopy, editPaste, editDelete;

    Menu helpMenu;
      MenuItem helpHelp;

    Scrollbar scrollbarV;
    Scrollbar scrollbarH;

    TextField message;

  private void buildWindow ( )  {

    // *** Instantiate objects

    outerBox = new MyFrame ( );

    centerPanel = new Panel ( );
    bottomPanel = new Panel ( );

    // *** Build Menus

    menuBar = new MenuBar ( );

    fileMenu = new Menu ("File");
    fileNew = new MenuItem ( "New" );
    fileMenu.add ( fileNew );
    fileNew.addActionListener ( this );
    fileOpen = new MenuItem ( "Open" );
    fileMenu.add ( fileOpen );
    fileOpen.addActionListener ( this );
    fileClose = new MenuItem ( "Close" );
    fileMenu.add ( fileClose );
    fileClose.addActionListener ( this );
    fileSave = new MenuItem ( "Save" );
    fileMenu.add ( fileSave );
    fileSave.addActionListener ( this );
    fileQuit = new MenuItem ( "Quit" );
    fileMenu.add ( fileQuit );
    fileQuit.addActionListener ( this );

    editMenu = new Menu ("Edit");
    editCut = new MenuItem ( "Cut" );
    editMenu.add ( editCut );
    editCut.addActionListener ( this );
    editCopy = new MenuItem ( "Copy" );
    editMenu.add ( editCopy );
    editCopy.addActionListener ( this );
    editPaste = new MenuItem ( "Paste" );
    editMenu.add ( editPaste );
    editPaste.addActionListener ( this );
    editDelete = new MenuItem ( "Delete" );
    editMenu.add ( editDelete );
    editDelete.addActionListener ( this );

    helpMenu = new Menu ("Help");
    helpHelp = new MenuItem ( "Help" );
    helpMenu.add ( helpHelp );
    helpHelp.addActionListener ( this );

    menuBar.add ( fileMenu );
    menuBar.add ( editMenu );
    menuBar.add ( helpMenu );
    menuBar.setHelpMenu ( helpMenu );

    // *** build the scrollbars

    scrollbarV = new Scrollbar (Scrollbar.VERTICAL, 0, 100, 0, 1000 );
    scrollbarV.addAdjustmentListener ( this );
    scrollbarH = new Scrollbar (Scrollbar.HORIZONTAL, 0, 100, 0, 1000 );
    scrollbarH.addAdjustmentListener ( this );

    // *** build the message display area

    message = new TextField ( "Messages Displayed Here", 80 );
    message.setEditable ( false );

    // *** build the bottom panel

    bottomPanel.setLayout ( new BorderLayout ( ) );
    bottomPanel.add ( "North", scrollbarH );
    bottomPanel.add ( "South", message );

    // *** Set colors

    centerPanel.setBackground (Color.white);
    bottomPanel.setBackground (Color.white);


    // *** build the window

    outerBox.setMenuBar ( menuBar );
    outerBox.setLayout (new BorderLayout ( ) );
    outerBox.add ("Center", centerPanel);
    outerBox.add ("South", bottomPanel);
    outerBox.add ("East", scrollbarV);
    outerBox.resize (600, 500);
    outerBox.setBackground (Color.white);

    outerBox.addWindowListener ( outerBox );

    outerBox.show( );

  }  // end buildWindow ()


  //****************** drawSomething

    // *** Global variables for drawing something

    int offsetX = 0;
    int offsetY = 0;
    int x = 300;
    int y = 250;

    private void drawSomething ()  {

        Graphics g = centerPanel.getGraphics ();
        Rectangle r = centerPanel.bounds ();
        g.setColor ( centerPanel.getBackground () );
        g.fillRect ( 0, 0, r.width, r.height );
        g.setColor ( new Color(170, 0, 0) );
        g.fillOval ( x + offsetX, y + offsetY, 200, 100 );

    }  // end drawSomething


  //***********  Interface Methods   ***********

  //****  ActionListener methods

  public void actionPerformed ( ActionEvent e )  {

    Object s = e.getSource();

    // *** process Menu actions

    if ( s instanceof MenuItem )  {

        // *** file menu items
        if ( s == fileNew )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileOpen )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileClose )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileSave )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileQuit )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }

        // *** edit menu items
        if ( s == editCut )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editCopy )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editPaste )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editDelete )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }

        // *** help menu items
        if ( s == helpHelp )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }

    }  // end process menu actions

  }  // end actionPerformed


  //****  AdjustmentListener method

  public void adjustmentValueChanged ( AdjustmentEvent e )  {

    Object s = e.getSource();

    // *** process Scrollbar actions

    if ( s instanceof Scrollbar )  {

        if ( s == scrollbarV )  {
            System.out.println ( "Vertical scrollbar detected; value = "+e.getValue() );
            offsetY = - scrollbarV.getValue();
            drawSomething ();
        }

        if ( s == scrollbarH )  {
            System.out.println ( "Horizontal scrollbar detected; value = "+e.getValue() );
            offsetX = - scrollbarH.getValue();
            drawSomething ();
        }

    }  // end process scrollbar actions

  }  // end AdjustmentListener


}// end step4


Run the applet