p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Do I need to to make an instance of the class to use the getBounds() method in the Elment class ?


Message #1 by dominskis@a... on Wed, 24 Apr 2002 21:44:38
 Dear People,
In doing Ivor Horton's "Beginning Java 2" jdk1.3  Chapter 14 "Drawing in a 
Window" page 708, I have an error in the element class in the following 
code:


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

the error message says:

"Element.java" : method getBounds(java.awt.Rectangle) not found in class 
sketchercalculator.Element.Text at line 182, column 22

do I need to make an instance of the class so getBounds(bounds) will be 
found in class? This is not what is done on page 730 to run the program.


Your assistance is really appreciated 
below is the element class
Stan


>
> import java.awt.*;
> import java.awt.geom.*;
>
>   //Sketcher version  #13 adding text to the Sketch
>   //deleting the getShape() method and adding the draw() method
>
>    public abstract class Element
>     {
>       public Element(Color color)
>        {
>          this.color = color;
>        }
>
>        public Color getColor()
>         {
>           return color;
>         }
>
>
>         public abstract java.awt.Rectangle getBounds();
>
>         protected Color color;
>
>         public abstract void modify(Point start, Point last);
>
>         //for version #13 for text drawing
>         //we will implement the draw() polymorphic method in each element
class
>         public abstract void draw(Graphics2D g2D);
>           public Line2D line;
>         //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);
>             }
>
>          //version #13 remove Shape() and insert draw()
>          public void draw(Graphics2D g2D)
>           {
>             g2D.setPaint(color);
>             g2D.draw(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
>                  }
>
>
>                    //version #13 remove Shape() and insert draw()
>                    public void draw(Graphics2D g2D)
>                     {
>                      g2D.setPaint(color);
>                      g2D.draw(line);
>                     }
>
>
>                     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);
>                   }
>
>                    //version #13 remove Shape() and insert draw()
>                    public void draw(Graphics2D g2D)
>                      {
>                        g2D.setPaint(color);
>                        g2D.draw(line);
>                      }
>
>
>                    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);
>                      }
>
>                      //version #13 remove Shape() and insert draw()
>                        public void draw(Graphics2D g2D)
>                           {
>                            g2D.setPaint(color);
>                            g2D.draw(line);
>                           }
>
>
>                      public java.awt.Rectangle getBounds()
>                       {
>                         return curve.getBounds();
>                       }
>
>                       private GeneralPath curve;
>                }//closing brace of the curve class
>
>        //Code defining text element
>         public static class Text extends Element
>          {
>           public Text(Font font, String text, Point position, Color 
color,
>            java.awt.Rectangle bounds)
>             {
>               super(color);
>               this.font = font;
>               this.position = position;
>               this.text = text;
>               this.bounds = bounds;
>               this.bounds.setLocation(position.x, position.y -
(int)bounds.getHeight());
>             }
>
>             public java.awt.Rectangle getBounds()
>              {
>               return getBounds(bounds);
>              }
>
>              public void draw(Graphics2D g2D)
>               {
>                 g2D.setPaint(color);
>
>                 //save the old font
>                 Font oldFont = g2D.getFont();
>
>                 //set the new font
>                 g2D.setFont(font);
>
>                 g2D.drawString(text, position.x, position.y);
>
>                 //restore the old font
>                 g2D.setFont(oldFont);
>               }
>
>               public void modify(Point start, Point last)
>                {
>                  //no code is required here but we must supply a
definition
>                }
>
>                //the font to be used
>                private Font font;
>
>                //the text to be displayed
>                private String text;
>
>                //the start of the text string
>                Point position;
>
>                //the bounding rectangle
>                java.awt.Rectangle bounds = null;
>             }
>
>
>           } //closing brace for the Element outer class
>



Here's what I think the problem is: The class text does have a getBounds
method, but that method itself is calling another
getBounds(java.awt.Rectangle) and that method does not exist in your class.
You should write another method public java.awt.Rectangle
getBounds(java.awt.Rectangle), or maybe you meant to call getBounds from
another class in which case you need to make a new instance of that class :
new ClassWithGetBounds..getBounds(bounds);
Message #2 by "Guy Worthington" <guyw@m...> on Fri, 26 Apr 2002 09:06:49
Stan wrote:

> In doing Ivor Horton's "Beginning Java 2" jdk1.3  Chapter 14 "Drawing in 
> a Window" page 708, I have an error in the element class in the 
> following code:
> 
>            public java.awt.Rectangle getBounds()
>             {
>              return getBounds(bounds);
>             }
>
> the error message says:
>
> "Element.java" : method getBounds(java.awt.Rectangle) not found in 
> class  sketchercalculator.Element.Text at line 182, column 22
>

You have already calculated the bounds object in the
Text() constructor.  Just return this object in the getter.
So the definition of the getBounds() method in the 
class Element.Text will look like:

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

  Return to Index