p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Only the fileOpen icon appears in Sketch Program !


Message #1 by dominskis@a... on Fri, 5 Apr 2002 18:33:23
Dear People,
 In doing Ivor Horton's "Beginning Java 2" Chapter 13 page 638 Sketcher 
program (adding a toolbar)... In Windows Explorer I dragged the Images 
folder from the WinZip Directory ( which was downlaoded from the wrox web 
site and  included all the images for this  program) to the Sketcher 
project. I run the program and there are no errors except that... 

only the file open icon shows ! Why are not the other images shouwing when 
they all exist in the Images directory of the Sketcher directory ?

Thank you in advance
below is the coding
Stan 


package sketcher;

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

  //Sketcher program version #7. adding a toolbar

  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 the open file image
        openAction.putValue(Action.SMALL_ICON, new ImageIcon
("Images/open.gif"));

        //add file buttons
        myToolBar.addSeparator();
        addToolBarButton(newAction);
        addToolBarButton(openAction);
        addToolBarButton(saveAction);
        addToolBarButton(printAction);

        //add element type buttons
        myToolBar.addSeparator();
        addToolBarButton(lineAction);
        addToolBarButton(rectangleAction);
        addToolBarButton(circleAction);
        addToolBarButton(curveAction);

        //add element color buttons
        myToolBar.addSeparator();
        addToolBarButton(redAction);
        addToolBarButton(yellowAction);
        addToolBarButton(greenAction);
        addToolBarButton(blueAction);
        myToolBar.addSeparator();

        //Create a toolbar border
        myToolBar.setBorder(BorderFactory.createCompoundBorder(
                            BorderFactory.createLineBorder(Color.darkGray),
                            BorderFactory.createEmptyBorder(2,2,4,2)));


          //inhibit (with false) the floating toolbar
          myToolBar.setFloatable(false);


         // version #7 add the toolbar to the content pane
         getContentPane().add(myToolBar, BorderLayout.NORTH);
     }

       //version #7 add inner classes defining action objects here....

       class FileAction extends AbstractAction
         {
           //constructor
           FileAction(String name)
            {
              super(name);
              String iconFileName = "Images/" + name + " .gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
             }

            //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;

        }
       //VERSION # 7 add inner class for the element type menus

       class TypeAction extends AbstractAction
        {
          TypeAction(String name, int typeID)
           {
             super(name);
             this.typeID = typeID;
             String iconFileName = "Images/" + name + " .gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
           }

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

            private int typeID;
         }


        // version # 7 add an inner class for the color type menus
        class ColorAction extends AbstractAction
         {
           public ColorAction(String name, Color color)
            {
              super(name);
              this.color = color;
              String iconFileName = "Images/" + name + " .gif";
              if(new File(iconFileName).exists())
               putValue(SMALL_ICON, new ImageIcon(iconFileName));
            }

            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();

        // version #7  create a toolbar
        private JToolBar myToolBar = new JToolBar();

          private JButton addToolBarButton(Action action)
           {
            //add a toolbar button
            JButton myJButton = myToolBar.add(action);

            // Add toolbar button border
            myJButton.setBorder(BorderFactory.createRaisedBevelBorder());

            //ensure no text on the button
            myJButton.setText(null);
            return myJButton;
          }


       //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;


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

//Sketcher Program version #7  adding a toolbar


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();
    }
}

package sketcher;

//Sketcher version #7  adding a toolbar

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;
}


  Return to Index