我尝试创建一个内部类来创建基于给定变量的三角形,然后在JavaFX场景中显示它。我的当前代码如下所示:
.count
}
当我调试代码时,它似乎突破了这一行:
public class OuterClass extends Application {
public class InnerClass extends Polygon {
Polygon triangle;
InnerClass(double x, double y, double side) {
double mySideSqrt = Math.sqrt((side * side) - (side / 2 * side / 2)) / 2;
double tX = x;
double tY = y - mySideSqrt;
double rX = x + side / 2;
double rY = y + mySideSqrt;
double lX = x - side / 2;
double lY = y + mySideSqrt;
Polygon triangle = new Polygon(tX, tY, rX, rY, lX, lY);
this.triangle = triangle;
}
}
@Override
public void start(Stage primaryStage) {
int myWidth = 600;
int myHeight = 519;
primaryStage.setTitle("Triangle");
Group root = new Group();
Scene scene = new Scene(root, myWidth, myHeight);
//Polygon triangle = new Polygon(100.0, 0.0, 200.0, 173.3, 0, 173.3); //100.0, 0.0, 200.0, 100.0, 0.0, 100.0
InnerClass triangle = new InnerClass(300, 260, 196);
triangle.setFill(Color.LIGHTGRAY);
root.getChildren().add(triangle);
primaryStage.setScene(scene);
primaryStage.show();
}
因为在内部类中创建的三角形不会被拉入方法,因此不会显示在场景中。
关于我如何让它发挥作用的任何想法?
答案 0 :(得分:2)
删除Polygon triangle;
并将Polygon triangle = new Polygon(tX, tY, rX, rY, lX, lY); this.triangle = triangle;
更改为getPoints().addAll(tX, tY, rX, rY, lX, lY);
全班:
public class InnerClass extends Polygon {
InnerClass(double x, double y, double side) {
double mySideSqrt = Math.sqrt((side * side) - (side / 2 * side / 2)) / 2;
double tX = x;
double tY = y - mySideSqrt;
double rX = x + side / 2;
double rY = y + mySideSqrt;
double lX = x - side / 2;
double lY = y + mySideSqrt;
getPoints().addAll(tX, tY, rX, rY, lX, lY);
}
}