在弧内复制并旋转JavaFX内容

时间:2017-09-05 22:21:10

标签: javafx

我正在尝试创建一个创建Mandalas的JavaFX应用程序(使用几何图案正方形,三角形,圆形的对称绘图)。我的计划是允许用户在一个弧上绘制形状。然后复制并旋转该弧以创建曼陀罗。

单弧: completed single arc

完成曼荼罗: completed mandala

我坚持如何制作成品曼荼罗。我正在考虑图层,但如何将画布的非弧区域设置为透明?有一个更好的方法吗?

到目前为止我的代码如下。

@Override
public void start(Stage primaryStage) 
{
    final double WIDTH = 400;       //constant for canvas size
    final double HEIGHT = 400; 

    StackPane root = new StackPane();
    Canvas canvas = new Canvas(WIDTH, HEIGHT);
    root.getChildren().add(canvas);
    Scene scene = new Scene(root, 400, 400);
    primaryStage.setTitle("Mandala");
    primaryStage.setScene(scene);
    primaryStage.show();

    GraphicsContext graphicsContext = canvas.getGraphicsContext2D();

    canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, 
        new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {
                graphicsContext.beginPath();
                graphicsContext.moveTo(event.getX(), event.getY());
                graphicsContext.stroke();
            }
        });

    canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, 
        new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {
                graphicsContext.lineTo(event.getX(), event.getY());
                graphicsContext.stroke();
            }
        });

    canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, 
        new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {

            }
        });

    int numSegments = 12;
    int numdegrees = 360/numSegments;
    int startAngle = 0;

      //  centreX, centreY, radiuisX (length of lines), radiusY, startAngle (0 is due east), numdegrees
    Arc arc1 = new Arc( 0, 0, 250, 250, startAngle, numdegrees);
    arc1.setType(ArcType.ROUND);
    arc1.setStroke(Color.BLACK);
    arc1.setFill(null);
    arc1.setStrokeWidth(1);
    root.getChildren().add(arc1);

}
}

1 个答案:

答案 0 :(得分:0)

使用剪辑路径。这样,剪辑路径之外的所有内容都将被忽略。此外,/** * @depends or/and @dataProvider test_getProject */ public function test_getWhere($dataToDepends) { // ... test ready ! } 有助于绘制旋转的对象。

transform

请注意,为简单起见,在绘图过程中不会显示这些线条。