Java FX 道路瓷砖

问题描述 投票:0回答:1

如何在 JavaFX 中绘制“Type 1”路砖,如图所示?它是一个四分之一圆,可根据角度(0、90、180、270)改变方向。任何有关如何实施这一点的指导将不胜感激!

在此输入图片描述

道路类型示例;

类型0

    public void drawType0Road(Group group, double cellWidth, double cellHeight, int rotation) {
        double roadWidth = cellWidth;
        double roadHeight = cellHeight - 10;
        double roadX = x * cellWidth;
        double roadY = y * cellHeight + 5;
        Rectangle road = new Rectangle(roadX, roadY, roadWidth, roadHeight);
        road.setFill(Color.web("#FEFEFE"));
        road.setRotate(rotation);
        group.getChildren().add(road);
    }

类型2

    public void drawType2Road(Group group, double cellWidth, double cellHeight, int rotation) {
        Group roadGroup;
        double padding = 0.2;
        double roadWidth = cellWidth;
        double roadHeight = cellHeight - (cellHeight * padding);
        double roadX = x * cellWidth;
        double roadY = y * cellHeight + (cellHeight * padding/2);
        Rectangle road = new Rectangle(roadX, roadY, roadWidth, roadHeight);
        road.setFill(Color.web("#FEFEFE"));

        double road2Width = cellWidth - (cellWidth * padding);
        double road2Height = cellHeight;
        double road2X = x * cellWidth + (cellWidth * padding/2);
        double road2Y = y * cellHeight;
        Rectangle road2 = new Rectangle(road2X, road2Y, road2Width, road2Height);
        road2.setFill(Color.web("#FEFEFE"));


        roadGroup = new Group(road, road2);
        roadGroup.setRotate(rotation);
        group.getChildren().add(roadGroup);
        
    }

我尝试使用圆形类型的圆弧来绘制它。但我无法给它我想要的形状。

java javafx traffic-simulation
1个回答
0
投票

我从这里更改了代码。

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

public class App extends Application {

    private static final double OUTER_RADIUS =300, INNER_RADIUS = 20, ORIGIN_X = 350, ORIGIN_Y = 350;
    
    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();    
        root.setLeft(new StackPane(getArc(0)));
        root.setTop(new StackPane(getArc(90)));
        root.setRight(new StackPane(getArc(180)));
        root.setBottom(new StackPane(getArc(270)));
        
        Scene scene = new Scene(root, 1000, 1000);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Group getArc(int rotation)
    {
        Point2D innerArcStart = getPoint(INNER_RADIUS, 90 + rotation);
        Point2D innerArcEnd = getPoint(INNER_RADIUS, 180 + rotation);
        Point2D outerArcStart = getPoint(OUTER_RADIUS, 90 + rotation);
        Point2D outerArcEnd = getPoint(OUTER_RADIUS, 180 + rotation);
        var path = getPath(innerArcStart, innerArcEnd, outerArcStart, outerArcEnd);
        
        return new Group(path);
    }
    
    private Point2D getPoint(double radius, double angle){
        double x = ORIGIN_X - radius * Math.cos(Math.toRadians(angle));
        double y = ORIGIN_Y - radius * Math.sin(Math.toRadians(angle));
        return new Point2D(x, y);
    }

    private Shape getPath(Point2D innerArcStart, Point2D innerArcEnd, Point2D outerArcStart, Point2D outerArcEnd){
        var path = new Path(
                new MoveTo(innerArcStart.getX(), innerArcStart.getY()),
                new LineTo(outerArcStart.getX(), outerArcStart.getY()), //left line
                new ArcTo(OUTER_RADIUS, OUTER_RADIUS, 0, outerArcEnd.getX(), outerArcEnd.getY(), false, true), //outer arc
                new LineTo(innerArcEnd.getX(),innerArcEnd.getY()), //right line
                new ArcTo(INNER_RADIUS, INNER_RADIUS, 0, innerArcStart.getX(), innerArcStart.getY(), false, false)
                );
        path.setFill(Color.color(Math.random(), Math.random(), Math.random()));
        return path;
    }
    
    public static void main(String args[]){
        launch(args);
    }
} 

输出

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.