p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Sketcher Program without Icons ! ! !


Message #1 by dominskis@a... on Fri, 5 Apr 2002 21:06:45
Dear People,
 Doing Ivor Horton's "Beginning Java 2" Chapter  13 page 641 Sketch program
 I have comleted the  program with no errors. I included the coding for 
toolbar and tooltips.

Yet no icon images are visible !

below is the coding 
thank you in advance
Stan


package sketcher;

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

  //Sketcher program version #8. adding  tooltips

  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), "Create new sketch");
      FileAction  openAction = new FileAction("Open",KeyStroke.getKeyStroke
('O', Event.CTRL_MASK), "Open existing sketch");
      FileAction  closeAction = new FileAction("Close", "Close sketch");
      FileAction  saveAction =  new FileAction
("Save",KeyStroke.getKeyStroke('S',Event.CTRL_MASK), "Save sketch");
       FileAction saveAsAction =    new FileAction("Save As...", "Save as 
new file");
       FileAction printAction = new FileAction("Print", 
KeyStroke.getKeyStroke('P', Event.CTRL_MASK), "Print sketch");

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

        //version #8 add a tooltip sring to   pass
        //we will add the pull down menu items using actions.....
        addMenuItem(myElementMenu, lineAction = new TypeAction
("Line",LINE, "Draw Lines"));
        addMenuItem(myElementMenu, rectangleAction = new TypeAction
("Rectangle",RECTANGLE, "Draw Rectangles"));
        addMenuItem(myElementMenu, circleAction = new TypeAction
("Circle",CIRCLE, "Draw Cirlies"));
        addMenuItem(myElementMenu, curveAction = new TypeAction
("Curve",CURVE, "Draw curves"));

        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, "Draw in red"));
         addMenuItem(colorMenu, yellowAction = new ColorAction
("Yellow",Color.yellow, "Draw in yellow"));
         addMenuItem(colorMenu, greenAction = new ColorAction
("Green",Color.green, "Draw in green"));
         addMenuItem(colorMenu, blueAction = new ColorAction
("Blue",Color.blue, "Draw in 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);
             }

              //version # 8 adding constructors
              FileAction(String name,KeyStroke keystroke, String tooltip)
               {
                this(name, keystroke);   //call the other constructor
                if(tooltip != null)
                  putValue(SHORT_DESCRIPTION, tooltip);
               }

              //takes care of a tooltip for an Action item without an 
accelerator keystroke
              FileAction(String name, String tooltip)
               {
                 this(name);     //call the other constructor
                 if(tooltip != null)
                  putValue(SHORT_DESCRIPTION, tooltip);
               }

             //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 #8 incorprorate the tooltip argunents
       //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));
           }

            //version #8 constructor add for the tooltip
            TypeAction(String name, int typeID, String tooltip)
             {
               this(name, typeID);
               if(tooltip != null)
                putValue(SHORT_DESCRIPTION, tooltip);
             }

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

            private int typeID;
         }

        //version #8 add a constructor for the tooltip
        // 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 ColorAction(String name, Color color, String tooltip)
             {
                this(name,color);
                if(tooltip != null)
                  putValue(SHORT_DESCRIPTION,tooltip);
             }

            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 #8  adding tooltips


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 #8  adding tooltips

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