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


Sign In
Create Account

Back to top









