p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Problem localized to the SketchFrame class but which line of code is causing the problem I do not know ! Help needed ! !


Message #1 by dominskis@a... on Wed, 17 Apr 2002 17:04:09
Dear Arthur Ore,
 your advice...". Try using each of their classes in turn,"
 worked ! I discovered the error is in the SketchFrame class.
But I don't know which line is causing the problem specifically.

 When I substituted this particular class into the project and swapped out 
my SketchFrame class, it worked and I am drawing lines and circles and 
rectangles and curves !
 But which line of code is causing the problem I do not know even after 
comparing my code with the downloaded code

In my coding I added the Status Bar and Modal dialog box from chapter 15 
before adding the sketching capability from chapter 14, but that shouldn't 
have caused the problem.

It is probable a hidden error somehow masked in a previous version of the 
Sketcher.
 I wish I knew which line of code is causing the problem.

below is first my version of SketchFrame, and then the correct downloaded 
version of SketchFrame 

Thank you in advance for helping me locate the varment bug 
Stan


//my version of SketchFrame

package sketchercalculator

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

  //Sketcher program version #12

  public  class SketchFrame extends JFrame implements 
Constants,ActionListener
   {
     private SketcherCalculator theApp;

     public SketchFrame(String title, SketcherCalculator theApp)
      {
        setTitle(title);

        this.theApp = theApp;

        setJMenuBar(myMenuBar);

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

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

        //part 2 create help manu
        JMenu helpMenu =  new JMenu("Help");

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

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

        // create a shortcuo for the  Help menu
        helpMenu.setMnemonic('H');

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

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

          //add the About item to the Help menu
          //create the item
          aboutItem = new JMenuItem("About");

          //the listener is the frame
          aboutItem.addActionListener(this);

          //add item to menu
          helpMenu.add(aboutItem);

          //add the help menu
          myMenuBar.add(helpMenu);

          //add the element menu items to the pop-up menu
          //popup.add(lineAction);
          //popup.add(rectangleAction);
          //popup.add(circleAction);
          //popup.add(curveAction);
          //popup.add(textAction);

          //popup.addSeparator();

          //popup.add(redAction);
          //popup.add(yellowAction);
          //popup.add(greenAction);
          //popup.add(blueAction);


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

          //  add the status pane to the content pane
          getContentPane().add(statusBar, BorderLayout.SOUTH);

          //Disable actions
          saveAction.setEnabled(false);
          closeAction.setEnabled(false);
          printAction.setEnabled(false);
     }

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


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

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

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

              //  to update the status bar when the element type changes
              statusBar.setTypePane(typeID);
            }

            private int typeID;
         }

        // add a constructor for the tooltip
        //  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;

              getContentPane().setBackground(color);

              // to update the status bar when the color element changes
              statusBar.setColorPane(color);
             }
            private Color color;
         }


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

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

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

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

          // define the status bar as a data member
          StatusBar statusBar = new StatusBar();

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

        //implementation to handle the About menu item's events
        public void actionPerformed(ActionEvent e)
         {
          if (e.getSource() == aboutItem)
           {
             //create about dialog with the app window as parent
            // JOptionPane.showMessageDialog(this, "About 
Sketcher", "Sketcher Copywrite Ivor Horton 2000", "About Sketcher", 
JOptionPane.INFORMATION_MESSAGE);

           }
         }

         public Color getElementColor()
          {
           return elementColor;
          }

          public int getElementType()
           {
             return elementType;
           }


        private JMenuItem aboutItem;

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

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

       //add a pop-up menu object
      // private JPopupMenu popup = new JPopupMenu("General");
    }  



//downloaded version of SketchFrame


package sketchercalculator;


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

public class SketchFrame extends JFrame implements Constants, 
ActionListener
{
  private SketcherCalculator theApp;

  // Constructor
  public SketchFrame(String title, SketcherCalculator theApp)
  {
    setTitle(title);                              // Set the window title

    this.theApp = theApp;

     // Add the menu bar to the window
    setJMenuBar(menuBar);

      // Default is exit the application
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // Create File menu
    JMenu fileMenu = new JMenu("File");

      // Create Elements menu
    JMenu elementMenu = new JMenu("Elements");

     // Create shortcut
    fileMenu.setMnemonic('F');

     // Create shortcut
    elementMenu.setMnemonic('E');

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

    // Construct the file pull down menu
    addMenuItem(fileMenu, newAction);
    addMenuItem(fileMenu, openAction);
    addMenuItem(fileMenu, closeAction);
    fileMenu.addSeparator();                                       // Add 
separator
    addMenuItem(fileMenu, saveAction);
    addMenuItem(fileMenu, saveAsAction);
    fileMenu.addSeparator();                              	   // Add 
separator
    addMenuItem(fileMenu, printAction);

    // Construct the Element pull down menu
    addMenuItem(elementMenu, lineAction = new TypeAction("Line", 
LINE, "Draw lines"));
    addMenuItem(elementMenu, rectangleAction = new  TypeAction
("Rectangle",RECTANGLE, "Draw rectangles"));
    addMenuItem(elementMenu, circleAction = new TypeAction("Circle", 
CIRCLE, "Draw circles"));
    addMenuItem(elementMenu, curveAction = new TypeAction("Curve", 
CURVE, "Draw curves"));

    elementMenu.addSeparator();

    JMenu colorMenu = new JMenu("Color");         // Color sub-menu
    elementMenu.add(colorMenu);                   // Add the sub-menu

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

    menuBar.add(fileMenu);                        // Add the file menu
    menuBar.add(elementMenu);                     // Add the element menu

    // Add file buttons
    toolBar.addSeparator();                                 // Space at 
the start
    addToolBarButton(newAction);
    addToolBarButton(openAction);
    addToolBarButton(saveAction);
    addToolBarButton(printAction);

    // Add element type buttons
    toolBar.addSeparator();
    addToolBarButton(lineAction);
    addToolBarButton(rectangleAction);
    addToolBarButton(circleAction);
    addToolBarButton(curveAction);

    // Add element color buttons
    toolBar.addSeparator();
    addToolBarButton(redAction);
    addToolBarButton(yellowAction);
    addToolBarButton(greenAction);
    addToolBarButton(blueAction);
    toolBar.addSeparator();                            		// Space 
at the end

    toolBar.setBorder(BorderFactory.createCompoundBorder(       // Toolbar 
border
                      BorderFactory.createLineBorder(Color.darkGray),
                      BorderFactory.createEmptyBorder(2,2,4,2)));

    toolBar.setFloatable(false);    				// Inhibit 
toolbar floating

    //add the About item to the Help menu
    //create the item
    aboutItem = new JMenuItem("About");

    //the listener is the frame
    aboutItem.addActionListener(this);

    //add item to the menu
    helpMenu.add(aboutItem);

    //add the help menu
     menuBar.add(helpMenu);


    getContentPane().add(toolBar, BorderLayout.NORTH); 		// Add the 
toolbar
    getContentPane().add(statusBar, BorderLayout.SOUTH);        // Add the 
statusbar


    // Disable actions
    saveAction.setEnabled(false);
    closeAction.setEnabled(false);
    printAction.setEnabled(false);

  }

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

    KeyStroke keystroke = (KeyStroke)action.getValue
(action.ACCELERATOR_KEY);
    if(keystroke != null)
      item.setAccelerator(keystroke);
    item.setIcon(null);
    return item;						// Return 
the menu item
  }

  //FILE ACTION INNER CLASS
  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);
    }

    // Constructor
    FileAction(String name, KeyStroke keystroke, String tooltip)
    {
      this(name, keystroke);                          // Call the other 
constructor
      if(tooltip != null)                             // If there is 
tooltip text
        putValue(SHORT_DESCRIPTION, tooltip);         // ...squirrel it 
away
    }

    // Constructor
    FileAction(String name, String tooltip)
    {
      this(name);                                     // Call the other 
constructor
      if(tooltip != null)                             // If there is 
tooltip text
        putValue(SHORT_DESCRIPTION, tooltip);         // ...squirrel it 
away
    }

    // Event handler
    public void actionPerformed(ActionEvent e)
    {
      // We will add action code here eventually...
    }
  }

  //TYPE ACTION INNER CLASS
  class TypeAction extends AbstractAction
  {
    // Constructor
    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));
    }

    // Constructor
    TypeAction(String name, int typeID, String tooltip)
    {
      this(name, typeID);
      if(tooltip != null)                               // If there is a 
tooltip
        putValue(SHORT_DESCRIPTION, tooltip);           // ...squirrel it 
away
    }

    public void actionPerformed(ActionEvent e)
    {
      elementType = typeID;
      statusBar.setTypePane(typeID);
    }

    pirivate int typeID;
  }

  //COLOR ACTION INNER CLASS
  class ColorAction extends AbstractAction
  {
    // Constructor
    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));
    }

    // Constructor
    public ColorAction(String name, Color color, String tooltip)
    {
      this(name, color);
      if(tooltip != null)                               // If there is a 
tooltip
        putValue(SHORT_DESCRIPTION, tooltip);           // ...squirrel it 
away
    }

    // Constructor
    public void actionPerformed(ActionEvent e)
    {
      elementColor = color;
      statusBar.setColorPane(color);
    }

    private Color color;
  }

  // File actions
  private FileAction newAction, openAction, closeAction, saveAction, 
saveAsAction, printAction;

  // Element type actions
  private TypeAction lineAction, rectangleAction, circleAction, 
curveAction;

  // Element color actions
  private ColorAction redAction, yellowAction,
                      greenAction, blueAction;

  private JMenuBar menuBar = new JMenuBar();               // Window menu 
bar
  private JToolBar toolBar = new JToolBar();               // Window 
toolbar
  StatusBar statusBar = new StatusBar();    		   // Window 
status bar

  private JButton addToolBarButton(Action action)
  {
    JButton button = toolBar.add(action);                     // Add 
toolbar button
    button.setBorder(BorderFactory.createRaisedBevelBorder()); // Add 
button border
    button.setText(null);                                     // No button 
text
    return button;
  }

  private Color elementColor = DEFAULT_ELEMENT_COLOR;      // Current 
element color
  private int elementType = DEFAULT_ELEMENT_TYPE;          // Current 
element type

  public Color getElementColor()
  {
    return elementColor;
  }

  public int getElementType()
  {
    return elementType;
  }
  private JMenuItem aboutItem;
}



"Arthur Ore" <arthur@j...> wrote:
>Stan,
>
>As I said in an earlier post, I'm a relative beginner to Java. I can't 
spot
>what's wrong with this, without spending hours debugging it.
>
>I've got a couple of suggestions
>
>1) make sure you have checked the latest errata from Wrox's web site and
>correct any code as per that.
>
>2) I've checked and their downloaded code from directory "fullcode\chapter
>14\12_DrawingCurves" works OK. Try using each of their classes in turn,
>instead of yours. When you've established which class the problem lies in,
>you can then replace each of the methods in your version of that class 
until
>your project works as expected. Hopefully you'll then spot what's missing.
>
>Maybe someone else can help you spot what's wrong, but there's an awful 
lot
>of code to wade through.
>
>Arth
>
>
>"Stanley Dominski" <dominskis@a...> wrote in message
>news:3cbc2d42$1_1@d...
>>
>> Dear Arthur,
>>   Thank you for your kind response. I went throught the entire
>> Chapter 14 "Drawing in a Window" chapter from Ivor Horton's "Beginning
>Java 2" JDK1.3 text (skipping the applet
>> parts).
>>   With your help I was able to complete the program correctly
>> and can now draw all kinds of shapes using lines.
>> It turns out that the  code I was missing that you identified
>> for me is  also missing from the book on page 700!
>>
>> public abstract void modify(Point start, point last);
>>
>>  I located it in the downloaded coding from the Wrox web site.
>>   Only two classes were altered to obtain the drawing ,
>> the SketchView class and mostly the Element class.
>>
>> Since then I added the Element.Rectangle class and Element.Circle class
>and Element.Curve class .
>>
>> I compared my Element class and the SketchView class with the
>> downloaded classes, and they appear identical. No other classes
>>  were altered so I did not compare them against the downloaded classes.
>>
>> Below are all classes beginning with Element and SketchView.
>>
>> Your assistance is greatly appreciated.
>> Stan
>>
>>>                      {
>>
>
>
>

  Return to Index