移动不起作用它只移动一次。当我移动图像时,那里有2个图像。怎么做?
GraphicsContext gc = canvas.getGraphicsContext2D();
Image img1 = new Image( "com/resources/aqua.jpg" );
gc.drawImage( img1, 0, 0, 50, 50 );
theScene.setOnKeyPressed((event) -> {
if (event.getCode() == KeyCode.RIGHT ){
gc.drawImage( img1, 50, 0, 50, 50 );
}
});
答案 0 :(得分:2)
如果您绘制新图像,则不会清除Canvas
。您必须使用clearRect
“手动”执行此操作,例如:
private double minX;
private double minY;
private double width;
private double height;
private GraphicsContext gc;
private Image img1;
private void drawImage() {
gc.drawImage(img1, minX, minY, width, height);
}
private void moveImage() {
gc.clearRect(minX, minY, width, height);
minX += 50;
drawImage();
}
...
this.gc = canvas.getGraphicsContext2D();
this.img1 = ...;
this.minX = 0;
this.minY = 0;
this.width = 50;
this.height = 50;
this.drawImage();
theScene.setOnKeyPressed((event) -> {
if (event.getCode() == KeyCode.RIGHT) {
moveImage();
}
});