You will need to make a new line for each side of the rectangle, then check collisions with each side. To make a simple method, you could have an array that contains each intersection with the rectangle. Top: point[0]
Bottom: point[1]
Left: point[2]
Right: point[3]
In your case you would need to take into account the direction the line was going so that you would know which side to go with; basically, if xo < rectangle.getX()
you go with the bottom side else
you go with the top side, if yo < rectangle.getY()
you go with the left side else
you go with the right side.
public Point2D[] getIntersectionPoint(Line2D line, Rectangle2D rectangle) {
Point2D[] point = new Point2D[4];
point[0] = getIntersectionPoint(line,new Line2D.Double(rectangle.getX(),rectangle.getY(),rectangle.getX() + rectangle.getWidth(),rectangle.getY()));
point[1] = getIntersectionPoint(line,new Line2D.Double(rectangle.getX(),rectangle.getY() + rectangle.getHeight(),rectangle.getX() + rectangle.getWidth(),rectangle.getY() + rectangle.getHeight()));
point[2] = getIntersectionPoint(line,new Line2D.Double(rectangle.getX(),rectangle.getY(),rectangle.getX(),rectangle.getY() + rectangle.getHeight()));
point[3] = getIntersectionPoint(line,new Line2D.Double(rectangle.getX() + rectangle.getWidth(),rectangle.getY(),rectangle.getX() + rectangle.getWidth(),rectangle.getY() + rectangle.getHeight()));
return p;
}