Jump to content

Extending Rectangle2D.Double & Inner class problem

- - - - -

  • Please log in to reply
3 replies to this topic

#1
adash

adash

    Newbie

  • Members
  • Pip
  • 6 posts
Hi Guys !

I'm kindly stucked at one place and I cannot solve it.

1) I would like to extend a Rectangle2D, which means I would like to have a rectangle but extended with some my proper variables (like distance from a point).

I have created a class :
public class rectangle extends Rectangle2D.Double  {


    public rectangle(double x, double y, double width, double height, Point2D center){

       this.x = x;

       this.y = y;

       this.width=width;

       this.height=height;

       this.center=center;

       

	setRect(new Rectangle2D.Double(x,y,width,height));

       

       distance = distanceP(center);

       

    }

    

    private double distanceP(Point2D p){

         double dist = -1, temp;

         Point2D[] distances = new Point2D[4];



         if(p.getX()>=getMinX() && p.getX()<=getMaxX())

            if(p.getY()<getMinY() && p.getY()<getMaxY())

                dist = p.distance(p.getX(),getMinY());

                    else

                dist = p.distance(p.getX(),getMaxY());

         else if(p.getY() >= getMinY() && p.getY() <= getMaxY())

             if(p.getX()<getMinX() && p.getX()<getMaxX())

                dist = p.distance(getMinX(),p.getY());

                    else

                dist = p.distance(getMaxX(),p.getY());

         else {


        distances[0] = new Point2D.Double(getMinX(),getMinY());

        distances[1] = new Point2D.Double(getMinX(),getMaxY());

        distances[2] = new Point2D.Double(getMaxX(),getMinY());

        distances[3] = new Point2D.Double(getMaxX(),getMaxY());


            for(int i=0;i<=3;++i){

            temp = p.distance(distances[i]);

            if(dist==-1) dist=temp;

            if(temp<dist)

                dist = temp;

         }

         }


         return dist;

    }


    private Point2D center;

    private double distance;

}

It throws an NullPointerException on the line :
if(p.getX()>=getMinX() && p.getX()<=getMaxX())
probably it is something wrong with this getMinX, but I don't know how to fix it.


My second doubt is
how to create an inner class in separate file in a package??? :confused::confused::confused:

I hope you could help me. Thanks in advance.

Adash

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
When extending a class, the first line of your constructor must call the extended class' constructor. You do this by using super()
In this case, your constructor will look like.

 
    public rectangle(double x, double y, double width, double height, Point2D center){
       super(x, y, width, height);       
       this.center=center;       
       distance = distanceP(center);       
    }

I'm not quite sure about your 2nd question.
An inner class is in another class, in the same file. that's why it's called "inner"...

#3
adash

adash

    Newbie

  • Members
  • Pip
  • 6 posts
Now my class looks like :

public class rectangle extends Rectangle2D.Double  {


    public rectangle(double x, double y, double width, double height, Point2D center){

       super(x,y,width,height);

       this.center=center;

       

       //this.position = new positionOnBoard();

       distance = distanceP(center);

       

    }

   

     private double distanceP(Point2D p){

         double dist = -1, temp;

         Point2D[] distances = new Point2D[4];



         if(p.getX()>=getMinX() && p.getX()<=getMaxX())

            if(p.getY()<getMinY() && p.getY()<getMaxY())

                dist = p.distance(p.getX(),getMinY());

                    else

                dist = p.distance(p.getX(),getMaxY());

         else if(p.getY() >= getMinY() && p.getY() <= getMaxY())

             if(p.getX()<getMinX() && p.getX()<getMaxX())

                dist = p.distance(getMinX(),p.getY());

                    else

                dist = p.distance(getMaxX(),p.getY());

         else {


        distances[0] = new Point2D.Double(getMinX(),getMinY());

        distances[1] = new Point2D.Double(getMinX(),getMaxY());

        distances[2] = new Point2D.Double(getMaxX(),getMinY());

        distances[3] = new Point2D.Double(getMaxX(),getMaxY());


            for(int i=0;i<=3;++i){

            temp = p.distance(distances[i]);

            if(dist==-1) dist=temp;

            if(temp<dist)

                dist = temp;

         }

         }


         return dist;

    }


    private Point2D center;

    private double distance;



}


When i try :
 rectangle r = new rectangle(100,150,50,100,ballCenter);
It still throws a nul pointer in the same place... :c-mad:

#4
adash

adash

    Newbie

  • Members
  • Pip
  • 6 posts
Ok. solved. Point p was null, I made a mistake...... Sorry




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users