简体   繁体   English

JavaFX Canvas始终在同一位置绘制

[英]JavaFX Canvas draw always in the same location

I'm trying to draw an airplane on a canvas and move it(Don't know if it matters - a second Canvas placed in the same location in a GridPane as the first Canvas which is drawned last, like in the picture) . 我正在尝试在画布上绘制飞机并移动它(不知道是否重要-将第二个Canvas与最后绘制的第一个Canvas放在GridPane中的相同位置,如图所示)。 I used GraphicContext appendSVGPath , and positined the airplane succesfully : 我使用了GraphicContext appendSVGPath,并将飞机成功放置:

在此处输入图片说明

I've added a button to test the movement by increasing x and y by 2 each click, triggering the redraw. 我添加了一个按钮来测试运动,方法是将x和y每次单击增加2,从而触发重绘。 the redraw indeed was triggered, and with the correct parameters, but the drawing stays the same. 重新绘制的确是触发的,并带有正确的参数,但是图形保持不变。 I tried testing what happens if in the second time clicked, I only run the clearRect command, and indeed the airplane was cleaned so I assume it just redraw it in the same location every time.. 我尝试测试如果第二次单击时只执行clearRect命令会发生什么情况,并且确实对飞机进行了清洁,因此我假设每次都将它重新绘制在同一位置。

What am I missing? 我想念什么?

public class AirplaneDrawer extends Canvas{
    public double x,y,rotate,lastX,lastY;
    public boolean isFirst = true;

    public void setCords(double x,double y,double rotate) {
        if(x != this.x || y != this.y || rotate != this.rotate) {
            lastX=this.x;
            lastY=this.y;
            this.x = x;
            this.y = y;
            this.rotate = rotate;
            redraw();
        }
    }

    public void redraw() {
        GraphicsContext gc = getGraphicsContext2D();
        String path = new String("M 323.193 226.645 c 39.244 24.41 75.644 47.053 115.706 71.978 c 7.687 -5.443 20.518 -14.485 33.308 -23.596 c 16.733 -11.923 36.27 -11.452 49.046 3.779 c 3.513 4.191 2.568 15.766 -0.705 21.426 c -23.705 40.994 -48.427 81.404 -73.095 121.833 c -4.729 7.745 -9.06 19.278 -21.177 13.509 c -12.203 -5.8 -28.746 -9.521 -27.842 -28.026 c 0.891 -18.185 3.495 -36.292 4.924 -50.249 c -40.704 -19.793 -79.74 -38.778 -119.825 -58.269 c -16.168 17.561 -22.275 40.532 -27.606 64.119 c -8.975 39.719 -18.474 79.324 -28.171 118.881 c -5.593 22.809 -12.452 26.109 -34.167 17.908 c -28.122 -10.606 -31.047 -14.689 -31.318 -45.384 c -0.605 -68.198 -1.514 -136.4 -1.325 -204.593 c 0.045 -15.865 -4.177 -25.531 -19.237 -32.95 c -30.238 -14.884 -60.119 -30.866 -88.548 -48.915 c -13.988 -8.884 -26.951 -21.77 -35.867 -35.727 C 3.526 110.834 15.381 90.43 40.637 91.746 c 17.786 0.931 36.644 4.67 52.618 12.229 c 32.58 15.413 63.735 33.905 95.022 51.921 c 8.735 5.028 15.083 4.992 23.944 0.068 c 64.671 -35.921 129.717 -71.172 194.611 -106.705 c 25.712 -14.075 46.608 -10.335 65.331 12.008 c 10.309 12.302 2.247 20.797 -6.506 28.579 c -35.89 31.91 -72.438 63.093 -107.682 95.687 C 344.877 197.641 334.677 212.878 323.193 226.645 Z");
        double wid = getWidth()/247;
        double hei = getHeight()/152;
        if(!isFirst) {
            gc.rotate(360-rotate);
            gc.scale(50, 50);
            gc.translate(-lastX*wid, -lastY*hei);
        }
        gc.clearRect(0, 0, getWidth(), getHeight());
        gc.translate(wid*x,hei*y);
        gc.scale(0.02,0.02);    
        gc.rotate(rotate);
        gc.appendSVGPath(path);
        gc.setFill(Color.BLACK);
        gc.fill();
        gc.stroke();
        isFirst = false;
    }
}

And the button handler in the controller : 以及控制器中的按钮处理程序:

public void btnClickHandler(MouseEvent event) {
    airplaneDrawer.setCords(airplaneDrawer.x+2, airplaneDrawer.y+2, 145);
}

Here is a part of the FXML file : 这是FXML文件的一部分:

  <MapDrawer fx:id="mapDrawer" height="237.0" width="256.0" GridPane.columnIndex="1" GridPane.rowIndex="1" >
    <GridPane.margin>
      <Insets left="18.0" top="-5" />
    </GridPane.margin>
  </MapDrawer>
  <AirplaneDrawer fx:id="airplaneDrawer" height="237.0" width="256.0" GridPane.columnIndex="1" GridPane.rowIndex="1" >
    <GridPane.margin>
      <Insets left="18.0" top="-5" />
    </GridPane.margin>
  </AirplaneDrawer>

在此处输入图片说明
This is the best I could get to make the plane move, You have to change some parameters to suit your application. 这是使飞机移动的最佳方法。您必须更改一些参数以适合您的应用程序。 What I am moving here is the canvas. 我要在这里移动的是画布。
I would suggest you create another canvas for the map. 我建议您为地图创建另一个画布。 (Not sure if this will work). (不确定这是否行得通)。 I still don't get why it's drawing on the same space but If you try to append another svg path after the translate (for example adding a path2 to the canvas in the if condition) you will see your second plane on another position. 我仍然不明白为什么它在相同的空间上绘制,但是如果您尝试在平移后附加另一个svg路径(例如,在if条件下向画布添加path2 ),则会在另一个位置看到第二个平面。

public void redraw() {
            GraphicsContext gc = getGraphicsContext2D();
    String path = new String("M 323.193 226.645 c 39.244 24.41 75.644 47.053 115.706 71.978 c 7.687 -5.443 20.518 -14.485 33.308 -23.596 c 16.733 -11.923 36.27 -11.452 49.046 3.779 c 3.513 4.191 2.568 15.766 -0.705 21.426 c -23.705 40.994 -48.427 81.404 -73.095 121.833 c -4.729 7.745 -9.06 19.278 -21.177 13.509 c -12.203 -5.8 -28.746 -9.521 -27.842 -28.026 c 0.891 -18.185 3.495 -36.292 4.924 -50.249 c -40.704 -19.793 -79.74 -38.778 -119.825 -58.269 c -16.168 17.561 -22.275 40.532 -27.606 64.119 c -8.975 39.719 -18.474 79.324 -28.171 118.881 c -5.593 22.809 -12.452 26.109 -34.167 17.908 c -28.122 -10.606 -31.047 -14.689 -31.318 -45.384 c -0.605 -68.198 -1.514 -136.4 -1.325 -204.593 c 0.045 -15.865 -4.177 -25.531 -19.237 -32.95 c -30.238 -14.884 -60.119 -30.866 -88.548 -48.915 c -13.988 -8.884 -26.951 -21.77 -35.867 -35.727 C 3.526 110.834 15.381 90.43 40.637 91.746 c 17.786 0.931 36.644 4.67 52.618 12.229 c 32.58 15.413 63.735 33.905 95.022 51.921 c 8.735 5.028 15.083 4.992 23.944 0.068 c 64.671 -35.921 129.717 -71.172 194.611 -106.705 c 25.712 -14.075 46.608 -10.335 65.331 12.008 c 10.309 12.302 2.247 20.797 -6.506 28.579 c -35.89 31.91 -72.438 63.093 -107.682 95.687 C 344.877 197.641 334.677 212.878 323.193 226.645 Z");

    double wid = getWidth()/247;
    double hei = getHeight()/152;

    if(!isFirst) {
        gc.rotate(360-rotate);
        gc.scale(50, 50);
        setTranslateX(-lastX*wid);
        setTranslateY(-lastY*hei);
        gc.setStroke(Color.BLUE);
        gc.stroke();

    }
    gc.clearRect(0, 0, getWidth(), getHeight());
    System.out.println("x="+x+" y="+y);
    setTranslateX(y*wid);
    setTranslateY(y*hei);
    gc.scale(0.02,0.02);
    gc.rotate(rotate);
    gc.appendSVGPath(path);
    gc.setFill(Color.BLACK);
    gc.fill();
    gc.stroke();

    isFirst = false;

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM