如何在 JavaFX 中将一个图像覆盖在另一个图像上并在 ActionTimer 中再次更改它?

How can I make one Image over another in JavaFX and change it again in an ActionTimer?

我想把正在上下移动的图像 "earth" 放在图像 "sun" 的前面,这样它就产生了 3d 效果。

我已经尝试过使用 setBlendMode,但显然它不适用于图像。

这是我的代码:

theStage.setTitle( "Timeline Example" );

        Group root = new Group();
        Scene theScene = new Scene( root );
        theStage.setScene( theScene );

        Canvas canvas = new Canvas( 512, 512 );
        root.getChildren().add( canvas );

        GraphicsContext gc = canvas.getGraphicsContext2D();

        Image earth = new Image( "/experiment/img/earth.png" );
        Image sun   = new Image( "/experiment/img/sun.png" );
        Image space = new Image( "/experiment/img/space.png" );

        final long startNanoTime = System.nanoTime();

        new AnimationTimer()
        {
            public void handle(long currentNanoTime)
            {
                double t = (currentNanoTime - startNanoTime) / 1000000000.0;

                double x = 232;
                double y = 232 + 128 * Math.sin(t);

                // background image clears canvas
                gc.drawImage( space, 0, 0 );
                gc.drawImage( earth, x, y );
                gc.drawImage( sun, 196, 196 );
            }
        }.start();

        theStage.show();
    }

每次太阳下山时,我怎样才能使地球在太阳上方?

earth.png

sun.png

space.png

我一直在寻找函数 toBack() 和 toFront() ,它们完全按照他们所说的去做。他们将 ImageView 或任何其他节点放在 back/foreground 中。而且我不再使用 canvas 了。感谢我的 java 老师,我找到了它们。