gpt4 book ai didi

JavaFX Canvas 重绘不执行任何操作

转载 作者:行者123 更新时间:2023-12-02 01:27:32 26 4
gpt4 key购买 nike

我有一个 Canvas ,我正在其上的特定位置绘制一架飞机(第一张图效果很好)。当我的程序运行时,我需要从另一台服务器采样飞机位置并相应地更新我的飞机位置。

一切正常(参数明智),除了没有任何内容被重绘..

编辑:更正 - 在原来的飞机顶部画了一点污迹,然后没有其他事情发生

我的代码:

public class AirplaneDrawer extends Canvas{
double x,y,rotate;

public void setCords(double x,double y,double rotate) {
if(x != this.x || y != this.y || rotate != this.rotate) {
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;
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();
}

}

我认为 GraphicContext 可能会记住最后一个缩放/平移/旋转,因此我尝试在 中的 if 开头添加第二个 if setCords 方法,如果不是第一次绘制,则向后撤消这些步骤,但它没有改变任何内容。

编辑:如果我使用此条件,那么我之前在第一次编辑中提到的污迹不会出现,并且不会重新绘制任何内容

        if(this.x!=0 || this.y!= 0 || this.rotate != 0) {
GraphicsContext gc = getGraphicsContext2D();
double wid = getWidth()/247;
double hei = getHeight()/152;
gc.rotate(-this.rotate);
gc.scale(50, 50);
gc.translate(-wid*this.x,-hei*this.y);
}

我做错了什么? (我已经调试并看到 setCords 正在根据需要使用正确的参数进行调用)

注意:我不知道这是否重要,但对 setCords 的调用是在单独的线程中进行的,因此我的程序将照常继续。

谢谢!

最佳答案

您提到:

Note: I dont know if it matter, but the call to setCords is being made in a separete Thread, so my program will continue as usual .

这确实很重要。来自GraphicsContext的文档:

A Canvas only contains one GraphicsContext, and only one buffer. If it is not attached to any scene, then it can be modified by any thread, as long as it is only used from one thread at a time. Once a Canvas node is attached to a scene, it must be modified on the JavaFX Application Thread.

Calling any method on the GraphicsContext is considered modifying its corresponding Canvas and is subject to the same threading rules.

如果您位于后台线程,则可以使用 Platform.runLater(Runnable) 将操作发布到 FX 线程。 。不过,请注意不要让 FX 线程充斥过多的操作。您可能需要考虑使用 AnimationTimer代替或可能结合(使用适当的同步)单独的线程。

此外,请注意,在 Canvas 上绘图不会清除之前绘制的任何内容。要模拟运动,您需要清除适当的区域(或在其上绘制,例如使用背景),然后在不同的位置重新绘制它。这可以是具体的,如仅清除/绘制更改的内容,也可以是一般的,如每次重新绘制整个 Canvas 。

关于JavaFX Canvas 重绘不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56670581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com