如何在JavaFX中绘制可旋转的Type 1四分之一圆弧道路瓦片?
JavaFX 绘制可旋转四分之一圆弧道路瓦片(Type 1)
我需要在JavaFX中绘制可按0、90、180、270度旋转的四分之一圆弧道路瓦片(Type 1),尝试用Arc组件绘制但没得到预期形状。以下是Type 0和Type 2道路瓦片的实现示例,求Type 1的实现指导:
Type 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); }
Type 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); }
Type 1 实现代码
以下是和现有风格统一的Type 1实现,通过Arc组件配合Group旋转实现四分之一圆弧道路:
public void drawType1Road(Group group, double cellWidth, double cellHeight, int rotation) { Group roadGroup = new Group(); double padding = 0.2; // 道路宽度,与Type2保持一致的内边距 double roadThickness = cellWidth * (1 - padding); // 圆弧圆心定位在瓦片的对应角上 double centerX = x * cellWidth + (cellWidth * padding / 2) + roadThickness; double centerY = y * cellHeight + (cellHeight * padding / 2) + roadThickness; // 初始为右→下的四分之一圆弧,通过旋转切换方向 Arc arc = new Arc(centerX, centerY, roadThickness, roadThickness, 0, 90); arc.setFill(Color.web("#FEFEFE")); // 设为ROUND类型,保持道路两端圆角风格 arc.setType(ArcType.ROUND); roadGroup.getChildren().add(arc); roadGroup.setRotate(rotation); group.getChildren().add(roadGroup); }
关键说明
- 沿用Type2的
padding参数,保证所有道路瓦片的宽度统一 Arc的startAngle=0、length=90对应初始的右转向下圆弧,通过Group的setRotate()可以直接切换0/90/180/270四个方向- 如果需要更精准的道路形状,也可以用
Path绘制圆弧加两段直边,但Arc配合ArcType.ROUND已能匹配现有实现的风格
内容的提问来源于stack exchange,提问作者Burakcan TOKSES
相关产品推荐
相关产品推荐

