Java Programming中的Point2D類
課本上的內容是這樣的
import java.util.Scanner;
import javafx.geometry.Point2D;
public class TestPoint2D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter point1's x-, y-coordinates:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter point2' s x-, y-coordinates:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point2D p1 = new Point2D(x1, y1);
Point2D p2 = new Point2D(x2, y2);
System.out.println("p1 is " + p1.toString());
System.out.println("p2 is " + p2.toString());
System.out.println("The distance between pi and p2 is" + p1.distance(p1);
}
}
在eclipse中編譯報錯,
不僅Point2D類不能實例化,聲明都不可以
這是怎麼回事呢,經過多次試驗,終於發現了問題所在,將上面的導入類javafx.geometry.Point2D換成java.awt.geom.Point2D;
此時便解決了聲明問題,但是還是無法實例化。
我找到了java的jdk中的文件
package java.awt.geom;
import java.io.Serializable;
/**
* The <code>Point2D</code> class defines a point representing a location
* in {@code (x,y)} coordinate space.
* <p>
* This class is only the abstract superclass for all objects that
* store a 2D coordinate.
* The actual storage representation of the coordinates is left to
* the subclass.
*
* @author Jim Graham
* @since 1.2
*/
public abstract class Point2D implements Cloneable {
/**
* The <code>Float</code> class defines a point specified in float
* precision.
* @since 1.2
*/
public static class Float extends Point2D implements Serializable {
/**
* The X coordinate of this <code>Point2D</code>.
* @since 1.2
* @serial
*/
public float x;
/**
* The Y coordinate of this <code>Point2D</code>.
* @since 1.2
* @serial
*/
public float y;
/**
* Constructs and initializes a <code>Point2D</code> with
* coordinates (0, 0).
* @since 1.2
*/
public Float() {
}
/**
* Constructs and initializes a <code>Point2D</code> with
* the specified coordinates.
*
* @param x the X coordinate of the newly
* constructed <code>Point2D</code>
* @param y the Y coordinate of the newly
* constructed <code>Point2D</code>
* @since 1.2
*/
public Float(float x, float y) {
this.x = x;
this.y = y;
}
它的構造函數中有Double, Float,使用的時候只需要用相應的構造函數。。
/** Float type */
System.out.print("Enter point1's x-, y-coordinates:");
float x1 = input.nextFloat();
float y1 = input.nextFloat();
System.out.print("Enter point2' s x-, y-coordinates:");
float x2 = input.nextFloat();
float y2 = input.nextFloat();
Point2D p1 = new Point2D.Float(x1,y1);
Point2D p2 = new Point2D.Float(x2, y2);
/** Double type */
System.out.print("Enter point1's x-, y-coordinates:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter point2' s x-, y-coordinates:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point2D p1 = new Point2D.Double(x1,y1);
Point2D p2 = new Point2D.Double(x2, y2);
這樣就解決了問題,TestPoint2D類就搞定了,以下是完整代碼
import java.util.Scanner;
import java.awt.geom.Point2D;
public class TestPoint2D {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter point1's x-, y-coordinates:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter point2' s x-, y-coordinates:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point2D p1 = new Point2D.Double(x1,y1);
Point2D p2 = new Point2D.Double(x2, y2);
System.out.println("p1 is " + p1.toString());
System.out.println("p2 is " + p2.toString());
System.out.println("The distance between pi and p2 is" + p1.distance(p1));
}
}
運行結果如下
Enter point1’s x-, y-coordinates: 2.5 7
Enter point2’ s x-, y-coordinates:3.5 12
p1 is Point2D.Double[2.5, 7.0]
p2 is Point2D.Double[3.5, 12.0]
The distance between p1 and p2 is 5.0990195135927845
以上就是解決問題的全部過程,如果有幸被您看到,非常感謝指正([email protected])