p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Sketcher program creates lines, but not rectangles, circles or curves !


Message #1 by dominskis@a... on Mon, 15 Apr 2002 19:40:25
Dear People,
 In doing Ivor Horton's "Beginning Java 2" chapter 14  page 708 I ran the 
Sketcher program with no errors and the program creates lines no problem.
But no rectangles, or circles or curves are created after choosing those 
options from the menu or toolbar.

There are no hints to the  problem because there are no error messages !
I checked my coding against the downloaded coding from Wrox and it appears 
ok.

below is the coding for the Element class and SketchView class
all other classes have no  changes
Thank you in advance
Stan

package sketchercalculator;

import java.awt.*;
import java.awt.geom.*;

  //Sketcher version  #12

   public abstract class Element
    {
      public Element(Color color)
       {
         this.color = color;
       }

       public Color getColor()
        {
          return color;
        }

        public abstract Shape getShape();
        public abstract java.awt.Rectangle getBounds();

        protected Color color;

        public abstract void modify(Point start, Point last);


        //Nested class defining a line
        public static class Line extends Element
         {
           public Line(Point start, Point end, Color color)
            {
              super(color);
              line = new Line2D.Double(start, end);
            }

           public Shape getShape()
            {
             return line;
            }


            public java.awt.Rectangle getBounds()
             {
               return line.getBounds();
             }

             public void modify(Point start, Point last)
              {
                line.x2 = last.x;
                line.y2 = last.y;
              }
              private Line2D.Double line;
           }

              //nested class defining a rectangle
              public static class Rectangle extends Element
               {
                public Rectangle(Point start, Point end, Color color)
                 {
                  super(color);
                  rectangle = new Rectangle2D.Double(
                  Math.min(start.x, end.x), Math.min(start.y, end.y),
                  Math.abs(start.x - end.x), Math.abs(start.y - 
end.y));  //width & height
                 }
                   public Shape getShape()
                    {
                     return rectangle;
                    }

                    public java.awt.Rectangle getBounds()
                     {
                      return rectangle.getBounds();
                     }

                     public void modify(Point start, Point last)
                      {
                       rectangle.x = Math.min(start.x, last.x);
                       rectangle.y = Math.min(start.y, last.y);
                       rectangle.width = Math.abs(start.x - last.x);
                       rectangle.height = Math.abs(start.y - last.y);
                      }

              private Rectangle2D.Double rectangle;
            }
              //Nested class defiing a circle
              public static class Circle extends Element
               {
                 public Circle(Point center, Point circumference, Color 
color)
                  {
                    super(color);
                    double radius = center.distance(circumference);
                    circle = new Ellipse2D.Double(center.x - radius, 
center.y - radius, 2.*radius, 2.*radius);
                  }

                  public Shape getShape()
                   {
                     return circle;
                   }

                   public java.awt.Rectangle getBounds()
                    {
                      return circle.getBounds();
                    }

                    public void modify(Point center, Point circumference)
                     {
                       double radius = center.distance(circumference);
                       circle.x = center.x -  (int)radius;
                       circle.y = center.y - (int)radius;
                       circle.width = circle.height = 2*radius;
                     }
                     private Ellipse2D.Double circle;

                }//closing brace for the circle inner class

                //Nested class defining a curve
                public static class Curve extends Element
                 {
                   public Curve(Point start, Point next, Color color)
                    {
                      super(color);
                      curve =  new GeneralPath();
                      curve.moveTo(start.x, start.y);

                      curve.lineTo(next.x, next.y);

                    }
                    //add another segment
                    public void modify(Point start, Point next)
                     {
                       curve.lineTo(next.x, next.y);
                     }

                    public Shape getShape()
                     {
                       return curve;
                     }

                     public java.awt.Rectangle getBounds()
                      {
                        return curve.getBounds();
                      }

                      private GeneralPath curve;
               }//closing brace of the curve class
          } //closing brace for the Element outer class

package sketchercalculator;

//Sketcher Program #12 with 4 versions of SketchView and Element classes

import java.awt.*;          //for graphics
import javax.swing.*;
import java.util.*;        //for Observer
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.event.*;   // for mouse input adapter

   class SketchView extends JComponent implements Observer,Constants
    {
       private SketcherCalculator theApp;

      public SketchView(SketcherCalculator theApp)
       {

       this.theApp = theApp;

       //create the mouse listener
       MouseHandler handler = new MouseHandler();

       //Listen for button events
       addMouseListener(handler);

       //Listen for motion events
       addMouseMotionListener(handler);
    }

     //Method called by observable object when it changes
        public void update (Observable o, Object rectangle)
     {
       if(rectangle == null)
        repaint();
        else
          repaint((Rectangle)rectangle);
     }

      public void paint(Graphics g)
       {
         //get a 2D device context
         Graphics2D g2D = (Graphics2D)g;
         Iterator elements = theApp.getModel().getIterator();
         Element element;

         //go through the list
         while(elements.hasNext())
          {
            //get the next element
            element = (Element)elements.next();

            //set the element color
            g2D.setPaint(element.getColor());

            //draw its shape
            g2D.draw(element.getShape());
          }
      }

         class MouseHandler extends MouseInputAdapter
          {
            public void mousePressed(MouseEvent e)
             {
               //save the cursor position
               start = e.getPoint();

                //get modifires
                int modifier = e.getModifiers();

                if((modifier & e.BUTTON1_MASK) != 0)
                 {
                   //get graphics context
                   g2D = (Graphics2D)getGraphics();

                   // set XOR mode
                   g2D.setXORMode(getBackground());

                   //set color
                   g2D.setPaint(theApp.getWindow().getElementColor());

                 }
              }


            public void mouseDragged(MouseEvent e)
             {
               last =   e.getPoint();
               int modifier = e.getModifiers();

               if((modifier & e.BUTTON1_MASK) != 0)
                {
                  //is there an element :
                  if(tempElement == null)
                     //no , so create one
                    tempElement =   createElement(start,last);
                  else
                   {
                     // yes - draw to erase it
                     g2D.draw(tempElement.getShape());

                     //now modify it
                     tempElement.modify(start,last);
                   }
                     //draw it
                     g2D.draw(tempElement.getShape());

                }
              }

                public void mouseReleased(MouseEvent e)
                 {
                   int modifier =  e.getModifiers();

                   if((modifier & e.BUTTON1_MASK) != 0)
                    {
                      if(tempElement != null)
                       {
                         // add element to the model
                         theApp.getModel().add(tempElement);

                         tempElement = null;

                       }

                       if(g2D != null)
                        {
                          g2D.dispose();
                          g2D = null;
                        }
                       //remove the points
                       start = last = null;
                   }
                }


             private Element createElement(Point  start, Point end)
              {
                switch(theApp.getWindow().getElementType())
                 {
                   case LINE:
                     return new Element.Line(start, end, theApp.getWindow
().getElementColor());

                     //version 2 Of Element class and SkecthView 
createElement() method changes...
                     // Take away the comment symbol for case RECTANGLE  
to activate the rectangle coding
                   case RECTANGLE:
                     return new Element.Rectangle(start, end, 
theApp.getWindow().getElementColor());

                     //version 3 Of Element class and SketchView 
createElement() method changes...
                   // take away the comment sysmol for case CIRCLE
                   case CIRCLE:
                     return new Element.Circle(start, end, theApp.getWindow
().getElementColor());

                      //version 4 Of Element class and SketchView 
createElement() method changes...
                   //take away the comment symbol for case CURVE
                   case CURVE:
                     return new Element.Curve(start, end, theApp.getWindow
().getElementColor());
               }
                 return null;
            } //end of class MouseHandler

    //temporary graphics context
    private Graphics2D g2D;

    // Stores cursor     position on press
    private Point start;

    //stores cursor position on drag
    private Point last;

    //stores a temporary element
    private Element tempElement;
 }
}


  Return to Index