将我的坐标转换为实际的JavaFx坐标。

huangapple go评论66阅读模式
英文:

Convert my coordinate to actual JavaFx coordinate

问题

我正在尝试制作一个名为Cascadia的游戏。由于每个图块都是六边形,为了表示它,我使用了一个哈希映射来存储点(坐标)和图块。

HashMap<Point, Tile> board;

这个点遵循这个结构

每个图块的宽度为100像素。
示例图块的示例

问题是如何将我在哈希映射中使用的这些点转换为场景上的实际坐标?

对于图块向左或向右移动,我没有问题,因为我只需添加或减去100像素。

英文:

I'm trying to build a game called Cascadia. Since every tile is a hexagon, to represent it I used a hashmap to store the point(coordinate) and the tile.

HashMap&lt;Point, Tile&gt; board;

The point follows this structure.

Each tile is a width of 100 pixels.
example of a tile.

The problem is how do I convert those points that I used in my hashmap and convert it into the actual coordinate on my scene?

I have no problem with the tiles going left or right since I can just add or subtract 100(pixels).

答案1

得分: 3

将坐标存储为字段

在这种方法中,Hexagon 实例有两个整数表示坐标。任何六边形都会将 x 和 y 打印为舞台标题,如下所示:

将我的坐标转换为实际的JavaFx坐标。

这是一个可以尝试的单一 JavaFX 应用程序

App.java

public class App extends Application {

    @Override
    public void start(Stage stage) {

        double HexagonRadius = 100;

        Hexagon hexagon1 = new Hexagon(1, 2, HexagonRadius, Color.CADETBLUE);

        Hexagon hexagon2 = new Hexagon(1, 0, HexagonRadius, Color.MEDIUMPURPLE);
        hexagon2.setTranslateY(hexagon1.getOffsetY() * 2);

        Hexagon hexagon3 = new Hexagon(1, 1, HexagonRadius, Color.MEDIUMSEAGREEN);
        hexagon3.setTranslateY(-hexagon1.getOffsetY() * 2);

        Hexagon hexagon4 = new Hexagon(0, 0, HexagonRadius, Color.CORNFLOWERBLUE);
        hexagon4.setTranslateY(-hexagon1.getOffsetY());
        hexagon4.setTranslateX(hexagon1.getOffsetX());

        Hexagon hexagon5 = new Hexagon(2, 2, HexagonRadius, Color.YELLOW);
        hexagon5.setTranslateY(-hexagon1.getOffsetY());
        hexagon5.setTranslateX(-hexagon1.getOffsetX());

        Hexagon hexagon6 = new Hexagon(2, 0, HexagonRadius, Color.ORANGE);
        hexagon6.setTranslateY(hexagon1.getOffsetY());
        hexagon6.setTranslateX(-hexagon1.getOffsetX());

        Hexagon hexagon7 = new Hexagon(2, 3, HexagonRadius, Color.SKYBLUE);
        hexagon7.setTranslateY(hexagon1.getOffsetY());
        hexagon7.setTranslateX(hexagon1.getOffsetX());

        Group hexagonsGroup
                = new Group(hexagon1, hexagon2, hexagon3, hexagon4, hexagon5, hexagon6, hexagon7);

        StackPane stackPane = new StackPane(hexagonsGroup);

        var scene = new Scene(stackPane, 640, 480);
        scene.setFill(Color.ANTIQUEWHITE);
        stage.setScene(scene);
        stage.show();
    }

    // ... 省略其他部分 ...

}
public final class Hexagon extends Group {

    // ... 省略其他部分 ...

    public Hexagon(int x, int y, double radius, Paint color) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        makeHexagon(radius, color);
        offsetY = calculateApothem();

        offsetX = radius * 1.5;
        changeTittle();

    }

    // ... 省略其他部分 ...

    public void changeTittle() {

        polygon.setOnMouseClicked(e -> {
            Stage stage = (Stage) this.getScene().getWindow();
            stage.setTitle("cordinate =" + x + "," + y);
        });

    }

    // ... 省略其他部分 ...

}
public static void main(String[] args) {
    launch();
}
英文:

Storing coordinates as field

In this approach Hexagon instances have two integers wich represent coords . Any hexagon will print x and y as stage tittle like so :

将我的坐标转换为实际的JavaFx坐标。

This is a single javafx app you can try

App.java

public class App extends Application {
@Override
public void start(Stage stage) {
double HexagonRadius = 100;
Hexagon hexagon1 = new Hexagon(1, 2, HexagonRadius, Color.CADETBLUE);
Hexagon hexagon2 = new Hexagon(1, 0, HexagonRadius, Color.MEDIUMPURPLE);
hexagon2.setTranslateY(hexagon1.getOffsetY() * 2);
Hexagon hexagon3 = new Hexagon(1, 1, HexagonRadius, Color.MEDIUMSEAGREEN);
hexagon3.setTranslateY(-hexagon1.getOffsetY() * 2);
Hexagon hexagon4 = new Hexagon(0, 0, HexagonRadius, Color.CORNFLOWERBLUE);
hexagon4.setTranslateY(-hexagon1.getOffsetY());
hexagon4.setTranslateX(hexagon1.getOffsetX());
Hexagon hexagon5 = new Hexagon(2, 2, HexagonRadius, Color.YELLOW);
hexagon5.setTranslateY(-hexagon1.getOffsetY());
hexagon5.setTranslateX(-hexagon1.getOffsetX());
Hexagon hexagon6 = new Hexagon(2, 0, HexagonRadius, Color.ORANGE);
hexagon6.setTranslateY(hexagon1.getOffsetY());
hexagon6.setTranslateX(-hexagon1.getOffsetX());
Hexagon hexagon7 = new Hexagon(2, 3, HexagonRadius, Color.SKYBLUE);
hexagon7.setTranslateY(hexagon1.getOffsetY());
hexagon7.setTranslateX(hexagon1.getOffsetX());
Group hexagonsGroup
= new Group(hexagon1, hexagon2, hexagon3, hexagon4, hexagon5, hexagon6, hexagon7);
StackPane stackPane = new StackPane(hexagonsGroup);
var scene = new Scene(stackPane, 640, 480);
scene.setFill(Color.ANTIQUEWHITE);
stage.setScene(scene);
stage.show();
}
public final class Hexagon extends Group {
private Polygon polygon;
private final double radius;
private final double radianStep = (2 * Math.PI) / 6;
private final int x;
private final int y;
private final double offsetY;
private final double offsetX;
public int getX() {
return x;
}
public int getY() {
return y;
}
public Hexagon(int x, int y, double radius, Paint color) {
this.x = x;
this.y = y;
this.radius = radius;
makeHexagon(radius, color);
offsetY = calculateApothem();
offsetX = radius * 1.5;
changeTittle();
}
private void makeHexagon(double radius, Paint color) {
polygon = new Polygon();
this.getChildren().add(polygon);
polygon.setFill(color);
polygon.setStroke(Color.WHITESMOKE);
polygon.setEffect(new DropShadow(10, Color.BLACK));
polygon.setStrokeWidth(10);
polygon.setStrokeType(StrokeType.INSIDE);
for (int i = 0; i &lt; 6; i++) {
double angle = radianStep * i;
polygon.getPoints().add(Math.cos(angle) * radius / 1.1);
polygon.getPoints().add(Math.sin(angle) * radius / 1.1);
}
}
public void changeTittle() {
polygon.setOnMouseClicked(e -&gt; {
Stage stage = (Stage) this.getScene().getWindow();
stage.setTitle(&quot;cordinate =&quot; + x + &quot;,&quot; + y);
});
}
public double getOffsetY() {
return offsetY;
}
public double getOffsetX() {
return offsetX;
}
private double calculateApothem() {
System.out.println(Math.tan(radianStep) * radius);
return (Math.tan(radianStep) * radius) / 2;
}
}
public static void main(String[] args) {
launch();
}
}

huangapple
  • 本文由 发表于 2023年2月24日 03:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定