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

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

Convert my coordinate to actual JavaFx coordinate

问题

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

  1. 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

  1. public class App extends Application {
  2. @Override
  3. public void start(Stage stage) {
  4. double HexagonRadius = 100;
  5. Hexagon hexagon1 = new Hexagon(1, 2, HexagonRadius, Color.CADETBLUE);
  6. Hexagon hexagon2 = new Hexagon(1, 0, HexagonRadius, Color.MEDIUMPURPLE);
  7. hexagon2.setTranslateY(hexagon1.getOffsetY() * 2);
  8. Hexagon hexagon3 = new Hexagon(1, 1, HexagonRadius, Color.MEDIUMSEAGREEN);
  9. hexagon3.setTranslateY(-hexagon1.getOffsetY() * 2);
  10. Hexagon hexagon4 = new Hexagon(0, 0, HexagonRadius, Color.CORNFLOWERBLUE);
  11. hexagon4.setTranslateY(-hexagon1.getOffsetY());
  12. hexagon4.setTranslateX(hexagon1.getOffsetX());
  13. Hexagon hexagon5 = new Hexagon(2, 2, HexagonRadius, Color.YELLOW);
  14. hexagon5.setTranslateY(-hexagon1.getOffsetY());
  15. hexagon5.setTranslateX(-hexagon1.getOffsetX());
  16. Hexagon hexagon6 = new Hexagon(2, 0, HexagonRadius, Color.ORANGE);
  17. hexagon6.setTranslateY(hexagon1.getOffsetY());
  18. hexagon6.setTranslateX(-hexagon1.getOffsetX());
  19. Hexagon hexagon7 = new Hexagon(2, 3, HexagonRadius, Color.SKYBLUE);
  20. hexagon7.setTranslateY(hexagon1.getOffsetY());
  21. hexagon7.setTranslateX(hexagon1.getOffsetX());
  22. Group hexagonsGroup
  23. = new Group(hexagon1, hexagon2, hexagon3, hexagon4, hexagon5, hexagon6, hexagon7);
  24. StackPane stackPane = new StackPane(hexagonsGroup);
  25. var scene = new Scene(stackPane, 640, 480);
  26. scene.setFill(Color.ANTIQUEWHITE);
  27. stage.setScene(scene);
  28. stage.show();
  29. }
  30. // ... 省略其他部分 ...
  31. }
  1. public final class Hexagon extends Group {
  2. // ... 省略其他部分 ...
  3. public Hexagon(int x, int y, double radius, Paint color) {
  4. this.x = x;
  5. this.y = y;
  6. this.radius = radius;
  7. makeHexagon(radius, color);
  8. offsetY = calculateApothem();
  9. offsetX = radius * 1.5;
  10. changeTittle();
  11. }
  12. // ... 省略其他部分 ...
  13. public void changeTittle() {
  14. polygon.setOnMouseClicked(e -> {
  15. Stage stage = (Stage) this.getScene().getWindow();
  16. stage.setTitle("cordinate =" + x + "," + y);
  17. });
  18. }
  19. // ... 省略其他部分 ...
  20. }
  1. public static void main(String[] args) {
  2. launch();
  3. }
英文:

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

  1. public class App extends Application {
  2. @Override
  3. public void start(Stage stage) {
  4. double HexagonRadius = 100;
  5. Hexagon hexagon1 = new Hexagon(1, 2, HexagonRadius, Color.CADETBLUE);
  6. Hexagon hexagon2 = new Hexagon(1, 0, HexagonRadius, Color.MEDIUMPURPLE);
  7. hexagon2.setTranslateY(hexagon1.getOffsetY() * 2);
  8. Hexagon hexagon3 = new Hexagon(1, 1, HexagonRadius, Color.MEDIUMSEAGREEN);
  9. hexagon3.setTranslateY(-hexagon1.getOffsetY() * 2);
  10. Hexagon hexagon4 = new Hexagon(0, 0, HexagonRadius, Color.CORNFLOWERBLUE);
  11. hexagon4.setTranslateY(-hexagon1.getOffsetY());
  12. hexagon4.setTranslateX(hexagon1.getOffsetX());
  13. Hexagon hexagon5 = new Hexagon(2, 2, HexagonRadius, Color.YELLOW);
  14. hexagon5.setTranslateY(-hexagon1.getOffsetY());
  15. hexagon5.setTranslateX(-hexagon1.getOffsetX());
  16. Hexagon hexagon6 = new Hexagon(2, 0, HexagonRadius, Color.ORANGE);
  17. hexagon6.setTranslateY(hexagon1.getOffsetY());
  18. hexagon6.setTranslateX(-hexagon1.getOffsetX());
  19. Hexagon hexagon7 = new Hexagon(2, 3, HexagonRadius, Color.SKYBLUE);
  20. hexagon7.setTranslateY(hexagon1.getOffsetY());
  21. hexagon7.setTranslateX(hexagon1.getOffsetX());
  22. Group hexagonsGroup
  23. = new Group(hexagon1, hexagon2, hexagon3, hexagon4, hexagon5, hexagon6, hexagon7);
  24. StackPane stackPane = new StackPane(hexagonsGroup);
  25. var scene = new Scene(stackPane, 640, 480);
  26. scene.setFill(Color.ANTIQUEWHITE);
  27. stage.setScene(scene);
  28. stage.show();
  29. }
  30. public final class Hexagon extends Group {
  31. private Polygon polygon;
  32. private final double radius;
  33. private final double radianStep = (2 * Math.PI) / 6;
  34. private final int x;
  35. private final int y;
  36. private final double offsetY;
  37. private final double offsetX;
  38. public int getX() {
  39. return x;
  40. }
  41. public int getY() {
  42. return y;
  43. }
  44. public Hexagon(int x, int y, double radius, Paint color) {
  45. this.x = x;
  46. this.y = y;
  47. this.radius = radius;
  48. makeHexagon(radius, color);
  49. offsetY = calculateApothem();
  50. offsetX = radius * 1.5;
  51. changeTittle();
  52. }
  53. private void makeHexagon(double radius, Paint color) {
  54. polygon = new Polygon();
  55. this.getChildren().add(polygon);
  56. polygon.setFill(color);
  57. polygon.setStroke(Color.WHITESMOKE);
  58. polygon.setEffect(new DropShadow(10, Color.BLACK));
  59. polygon.setStrokeWidth(10);
  60. polygon.setStrokeType(StrokeType.INSIDE);
  61. for (int i = 0; i &lt; 6; i++) {
  62. double angle = radianStep * i;
  63. polygon.getPoints().add(Math.cos(angle) * radius / 1.1);
  64. polygon.getPoints().add(Math.sin(angle) * radius / 1.1);
  65. }
  66. }
  67. public void changeTittle() {
  68. polygon.setOnMouseClicked(e -&gt; {
  69. Stage stage = (Stage) this.getScene().getWindow();
  70. stage.setTitle(&quot;cordinate =&quot; + x + &quot;,&quot; + y);
  71. });
  72. }
  73. public double getOffsetY() {
  74. return offsetY;
  75. }
  76. public double getOffsetX() {
  77. return offsetX;
  78. }
  79. private double calculateApothem() {
  80. System.out.println(Math.tan(radianStep) * radius);
  81. return (Math.tan(radianStep) * radius) / 2;
  82. }
  83. }
  84. public static void main(String[] args) {
  85. launch();
  86. }
  87. }

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:

确定