How can I draw a ‘Type 1’ roadtile in JavaFX as shown in the image? It’s a quarter circle that changes orientation based on degrees (0, 90, 180, 270). Any guidance on how to implement this would be greatly appreciated!
Example Road Types;
Type0
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);
}
Type2
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);
}
I tried to draw it using Arc of the round type. But I couldn't give it the shape I wanted.
I altered the code from here.
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);
}
}
Output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With