【发布时间】:2020-11-23 23:58:08
【问题描述】:
免责声明:我正在使用 Java 和 Javafx 11。只是把它放在那里 :)
我正在尝试为Logo 创建解释器,但遇到了障碍。你看,我默认使用画布来显示我需要的所有东西,因为这适合我正在做的事情。但是,我没有考虑到我的 Turtle 需要移动。
private void drawTurtle()
{
vertices[0] = new Vector2(position.x, position.y + 15); // The three points that make the triangle that is the turtle
vertices[1] = new Vector2(position.x - 15, position.y);
vertices[2] = new Vector2(position.x + 15, position.y);
vertices[1] = Renderer.rotatePoint(vertices[1], position, rotation); // applying rotation to vertices
vertices[2] = Renderer.rotatePoint(vertices[2], position, rotation);
vertices[0] = Renderer.rotatePoint(vertices[0], position, rotation);
Renderer.drawLine(vertices[2], vertices[1], currentPen); // drawing the vertices
Renderer.drawLine(vertices[2], vertices[0], currentPen);
Renderer.drawLine(vertices[1], vertices[0], currentPen);
}
为了在不留下“痕迹”的情况下实现这一点,我尝试通过用白色笔在其上绘制来擦除现有的海龟。这给了我……奇怪的结果。
然后我在这里看到一篇关于 SO 的帖子,其中谈到如果我想移动东西,我应该如何在窗格上使用 Line 对象。好吧,我尝试将它与画布结合起来制作 CanvasPane:
public class CanvasPane extends Pane
{
public final Canvas canvas;
public CanvasPane(double width, double height)
{
setWidth(width);
setHeight(height);
canvas = new Canvas(width, height);
getChildren().add(canvas);
canvas.widthProperty().bind(this.widthProperty()); // Change this so this canvas does not scale with the pane, and its size is constant.
canvas.heightProperty().bind(this.heightProperty());
}
}
并向其中添加了线对象,这样我就可以编辑它们的开始和结束值以使乌龟移动,但是我什么也没得到,没有线可以显示,而且我很困惑,不知道该做什么做。伟大的互联网上也没有任何帮助我,所以我现在问这个问题,看看是否有人对我如何完美地移动我的乌龟有想法。不,我不能使用clearRect()
TLDR:我的乌龟在画布上移动时留下痕迹,使用 Line and Pane 不起作用,我无法在画布上使用 clearRect()。救命!
【问题讨论】: