JavaFX: Creating a board grid with Permanin twist

谁说我不能喝 提交于 2020-03-05 06:08:29

问题


I am creating a board game (first ported to JavaFX) in which the player must kill the opponent's piece going through a loop.

The above is provided on the Wikipedia page for Surakurta (another name for Permanin). But, I have been only able to build a grid of this sort:

How can I create those roundabouts in the corners?

Implementation details for already built grid: GridPane filled with 36 BoardInput extends javafx.scene.control.Button objects. These objects are special because they automatically create a Background with three BackgroundFill objects - horizontal line, vertical line, and the pebble circular fill.


回答1:


Use a Path. ArcTo elements allow you to create the circular parts. HLineTo, VLineTo and ClosePath can be used for the straight sections:

Furthermore I don't recommend using BackgroundFills. I'd prefer overlaying invisible buttons on top of the board visuals or handling MouseEvents for the GridPane itself.

Example

private static ArcTo createArc(double radius, double dx, double dy) {
    ArcTo result = new ArcTo(radius, radius, 0, dx, dy, true, true);
    result.setAbsolute(false);
    return result;
}

private static HLineTo createHLine(double length) {
    HLineTo result = new HLineTo(length);
    result.setAbsolute(false);
    return result;
}

private static VLineTo createVLine(double length) {
    VLineTo result = new VLineTo(length);
    result.setAbsolute(false);
    return result;
}

private static Path createPath(double radius, double midSize, Color storke) {
    final double lineLength = 2 * radius + midSize;

    Path result = new Path(
            new MoveTo(radius, 2 * radius), // start at left end of top horizontal line
            createArc(radius, radius, -radius), // top left loop
            createVLine(lineLength), // down
            createArc(radius, -radius, -radius), // bottom left loop
            createHLine(lineLength), // right
            createArc(radius, -radius, radius), // bottom right loop
            createVLine(-lineLength), // up
            createArc(radius, radius, radius),
            new ClosePath() // left
    );
    result.setStroke(storke);
    result.setStrokeWidth(10);
    return result;
}

@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(new StackPane(
            createPath(100, 50, Color.GREEN),
            createPath(50, 150, Color.AQUA)
    ));
    primaryStage.setScene(scene);
    primaryStage.show();
}

Output



来源:https://stackoverflow.com/questions/52113774/javafx-creating-a-board-grid-with-permanin-twist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!