p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Sketcher program now Works !


Message #1 by dominskis@a... on Thu, 4 Apr 2002 03:52:26
Dear People,
 The Sketcher program (Ivor Horton's "Beginning Java 2" chapter 13 
page 630 ) now works. The problem I had was commenting out the event 
handling method actionPerformed. I thought I should comment it out because 
it had no code in it.It is just the method header
public void actionPerformed(ActionEvent e)
 { }

below is the code that works
Thank you 
Stan


package sketcher;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.border.*;


  //Sketcher program version #6. Menus set up using action objects
  // in preparation to add toolbars

  public     class SketchFrame extends JFrame implements Constants
   {
     public SketchFrame(String title)
      {
        setTitle(title);
        setJMenuBar(myMenuBar);

        //create the file menu
        JMenu myFileMenu = new JMenu("File");

        //create the element menu
        JMenu myElementMenu = new JMenu("Elements");

        //create a shortcut for the file menu
        myFileMenu.setMnemonic('F');

        //create a shortcut for the element menu
        myElementMenu.setMnemonic('E');

        //add a suitable Action object to a menu
        //and a helper method to economize on the numbe of lines of code

        //create the action items for the file menu
      FileAction  newAction = new FileAction("New",KeyStroke.getKeyStroke
('N',Event.CTRL_MASK));
      FileAction  openAction = new FileAction("Open",KeyStroke.getKeyStroke
('O', Event.CTRL_MASK));
      FileAction  closeAction = new FileAction("Close");
      FileAction  saveAction =  new FileAction
("Save",KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
       FileAction saveAsAction =    new FileAction("Save As...");
       FileAction printAction = new FileAction("Print", 
KeyStroke.getKeyStroke('P', Event.CTRL_MASK));

        //construct the file pull down menu using actions.....
          addMenuItem(myFileMenu, newAction);
          addMenuItem(myFileMenu, openAction);
          addMenuItem(myFileMenu, closeAction);
          myFileMenu.addSeparator();
          addMenuItem(myFileMenu, saveAction);
          addMenuItem(myFileMenu,saveAsAction);
          myFileMenu.addSeparator();
          addMenuItem(myFileMenu, printAction);


        //we will add the pull down menu items using actions.....
        addMenuItem(myElementMenu, lineAction = new TypeAction
("Line",LINE));
        addMenuItem(myElementMenu, rectangleAction = new TypeAction
("Rectangle",RECTANGLE));
        addMenuItem(myElementMenu, circleAction = new TypeAction
("Circle",CIRCLE));
        addMenuItem(myElementMenu, curveAction = new TypeAction
("Curve",CURVE));

        myElementMenu.addSeparator();

        //add color submenu
        JMenu colorMenu = new JMenu("Color");
        myElementMenu.add(colorMenu);


        // we will add the color menu items here using actions....
        addMenuItem(colorMenu, redAction = new ColorAction
("Red",Color.red));
         addMenuItem(colorMenu, yellowAction = new ColorAction
("Yellow",Color.yellow));
         addMenuItem(colorMenu, greenAction = new ColorAction
("Green",Color.green));
         addMenuItem(colorMenu, blueAction = new ColorAction
("Blue",Color.blue));

        myMenuBar.add(myFileMenu);
        myMenuBar.add(myElementMenu);
      }

       //add inner classes defining action objects here....

       class FileAction extends AbstractAction
         {
           //constructor
           FileAction(String name)
            {
              super(name);
            }

            //constructor
            FileAction(String name, KeyStroke keystroke)
             {
               this(name);
               if(keystroke != null)
                 putValue(ACCELERATOR_KEY, keystroke);
             }

             //Event handler
             public void actionPerformed(ActionEvent e)
              {
                //action code added later...
              }


            //add action objects as members here.......
             private FileAction newAction, openAction, closeAction, 
saveAction,
                     saveAsAction,printAction;

        }
       //add inner class for the element type menus

       class TypeAction extends AbstractAction
        {
          TypeAction(String name, int typeID)
           {
             super(name);
             this.typeID = typeID;
           }

           public void actionPerformed(ActionEvent e)
            {
             int  elementType;
              elementType =  typeID;
            }

            private int typeID;
         }


        //add an inner class for the color type menus
        class ColorAction extends AbstractAction
         {
           public ColorAction(String name, Color color)
            {
              super(name);
              this.color = color;
            }

            public void actionPerformed(ActionEvent e)
             {
              Color elementColor;
              elementColor = color;

              //this is temporary . Just to show it works
              getContentPane().setBackground(color);

             }
            private Color color;
         }

        //add helper method
        private JMenuItem addMenuItem(JMenu menu, Action action)
         {
           //add the menu item
           JMenuItem item = menu.add(action);

           KeyStroke keystroke = (KeyStroke)action.getValue
(action.ACCELERATOR_KEY);
           if(keystroke != null)
            item.setAccelerator(keystroke);

            return item;
         }

              //declare colorActionreference variables
        private ColorAction redAction;
        private ColorAction yellowAction;
        private ColorAction greenAction;
        private ColorAction blueAction;

        //for the members that will store references to the TypeAction 
objects
        private TypeAction lineAction, rectangleAction, circleAction, 
curveAction;

       //create window menu bar
       private JMenuBar myMenuBar = new JMenuBar();

       //variable to hold the current element color
       private Color myElementColor = DEFAULT_ELEMENT_COLOR;

       //variable to hold the current element type
       private int myElementTYPE =  DEFAULT_ELEMENT_TYPE;
     }


package sketcher;

//Sketcher version #6  Menus set up using action objects
  // in preparation to add toolbars


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

public interface Constants
{
  //Element type definitions
  int LINE = 101;
  int RECTANGLE = 102;
  int CIRCLE = 103;
  int CURVE = 104;

  //Initial conditions
  int DEFAULT_ELEMENT_TYPE = LINE;
  Color DEFAULT_ELEMENT_COLOR = Color.blue;
}



package sketcher;

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

//Sketcher Program version #6  Menus set up using action objects
  // in preparation to add toolbars


public class Sketcher
{
  //declare the application window reference variable as private
  private static SketchFrame myJFrame;

  //declare the application window object
  private static Sketcher theApp;

  public Sketcher()
   {
     myJFrame =  new SketchFrame("Sketcher , Enjoying the design process");

     myJFrame.setSize(400,400);

     Dimension myFrameSize = myJFrame.getSize();
     Toolkit myToolkit = myJFrame.getToolkit();
     Dimension myScreenSize = myToolkit.getDefaultToolkit().getScreenSize
();

     //center the frame on the screen
     myJFrame.setLocation((myScreenSize.width -  myFrameSize.width)/2,
                         (myScreenSize.height - myFrameSize.height)/2);

    //charnge the backgroud color of the content pane
    myJFrame.getContentPane().setBackground(Color.yellow);

    //create the WindowAdapter object that is the listener for the 
myJFrame object
    myJFrame.addWindowListener(new WindowHandler() );

    myJFrame.setVisible(true);
   }

   //Handler class for window events
   class WindowHandler extends WindowAdapter
    {
      //Handler method for window closing event
        public void windowClosing(WindowEvent e)
           {
             //release window resources
             myJFrame.dispose();

             //end the application
             System.exit(0);
           }
    }

   public static void main(String[] args)
    {
      new Sketcher();
    }
}

  Return to Index