在javafx中将canvas和pane与line对象集成

qnyhuwrf  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(372)

免责声明:我使用的是java和JavaFX11。把它放出来:)
我在试图为logo创建一个解释程序的过程中,但是遇到了一个障碍。你看,我默认使用画布来显示所有我需要的东西,因为这适合我正在做的事情。然而,我没有考虑到我的乌龟需要移动这个事实。

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);
}



由于实时旋转海龟而留下的痕迹。
为了做到这一点而不留下“痕迹”,我试图抹去现有的海龟画用白色钢笔在它。这让我。。。奇怪的结果。

这是在海龟旋转360度之后。
然后我在这里看到一个帖子,说如果我想移动东西,我应该如何使用窗格上的线条对象。我试着把它和画布结合起来做成画布:

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:我的乌龟在画布上移动时留下痕迹,使用线条和窗格不起作用,我不能使用 clearRect() 在我的画布上。救命啊!

9rnv2umw

9rnv2umw1#

使用一个窗格同时容纳canvas节点和“turtle”节点。

Canvas canvas = new Canvas(640, 480);
    Shape turtle = new Polygon(); // fill in the points
    Pane p = new Pane(canvas, turtle);

现在可以通过设置布局坐标或应用转换来控制turtle节点的位置。由于它是最后添加的,它将被绘制在画布上(您还可以使用stackpane使分层更加明确。)

相关问题