Canvas JavaFX画布使用固定中心旋转图像(且不反弹)
我试着用旋转的转子/头来编程一台风车。茎本身是固定的,并作为背景绘制在画布上(javafx)。转子/磁头本身是另一个图像。我想我可以旋转图像本身并将其绘制到graphicscontext上。(不起作用)然后我尝试了各种方法: a。将图像绘制到另一个画布上,旋转画布,制作该画布的快照。(不起作用) b。制作一个imageview,旋转imageview,对其进行快照并绘制(不起作用),然后 c。我试着做一个旋转转换(没有成功)。现在我回到b:我旋转的图像的图像视图 b类作品,类!它不知怎么地“反弹”,我不知道为什么,因为文档上说ImageView.setRotate(..)围绕中心旋转图像。图像本身具有相同的高度和长度,因此它应该像矩形一样反弹。。我只想让这种反弹停止 所有这些失败的尝试都来自于本论坛的阅读 或作为文本:Canvas JavaFX画布使用固定中心旋转图像(且不反弹),canvas,javafx,rotation,imageview,bounce,Canvas,Javafx,Rotation,Imageview,Bounce,我试着用旋转的转子/头来编程一台风车。茎本身是固定的,并作为背景绘制在画布上(javafx)。转子/磁头本身是另一个图像。我想我可以旋转图像本身并将其绘制到graphicscontext上。(不起作用)然后我尝试了各种方法: a。将图像绘制到另一个画布上,旋转画布,制作该画布的快照。(不起作用) b。制作一个imageview,旋转imageview,对其进行快照并绘制(不起作用),然后 c。我试着做一个旋转转换(没有成功)。现在我回到b:我旋转的图像的图像视图 b类作品,类!它不知怎么地“反弹
package sample;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Pane root;
private Canvas canvas;
private GraphicsContext gc;
SnapshotParameters params = new SnapshotParameters();
private final Image stem = new Image(getClass().getResource("windrad.png").toExternalForm());
private final ImageView wheel = new ImageView(new Image(getClass().getResource("rad.png").toExternalForm()));
private final int height = 720;
private final int width = 720;
private final double distanceWidth = 400.0 / 720.0;
private final double distanceHeight = 240.0 / 720.0;
private int degrees = 0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
params.setFill(Color.TRANSPARENT);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("sample.fxml"));
try {
root = fxmlLoader.load();
canvas = (Canvas) fxmlLoader.getNamespace().get("canvas");
canvas.setHeight(height);
canvas.setWidth(width);
primaryStage.setHeight(height);
primaryStage.setWidth(width);
gc = canvas.getGraphicsContext2D();
} catch (IOException e) {
e.printStackTrace();
}
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
animation().start();
}
private AnimationTimer animation() {
return new AnimationTimer() {
@Override
public void handle(long now) {
gc.clearRect(0,0, width, height);
gc.drawImage(stem, 0, 0);
degrees = degrees >= 360 ? 0 : ++degrees;
wheel.setRotate(degrees);
gc.drawImage(wheel.snapshot(params, null), width * distanceWidth - (int) wheel.getImage().getWidth() / 2, height * distanceHeight - (int) wheel.getImage().getHeight() / 2);
}
};
}
}
好的,我让它工作了@詹姆斯帮了我很多忙。我没有把它画在画布上,而是把所有的对象放在一个场景图中,然后把它移到那里。因为我的转子/头部是imageview,所以我能够使用imageview.setRotate()。我的问题(为此我发布了这个帖子)是由画布的使用引起的。我仍然不知道弹跳错误是如何发生的,但我的项目目标已经实现了。新的源代码在答案中
我的意思是在场景图中放置节点,而不是在画布上绘制。当然,您正在(当然)将画布放置在场景图中。但一旦画出画布,就很难修改它@詹姆斯·杜德
这个评论帮助并解决了我的问题。问题中列出了新的源代码。我没有将其绘制到画布上,而是将所有对象放在场景图中,并移动imageview,而不是画布或图像。以下是新的源代码:
package sample;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class Main extends Application {
private Group root;
private final ImageView stem = new ImageView(new Image(getClass().getResource("windrad.png").toExternalForm()));
private final ImageView wheel = new ImageView(new Image(getClass().getResource("rad.png").toExternalForm()));
private final int height = 720;
private final int width = 720;
private final double distanceWidth = 360.0 / 720.0;
private final double distanceHeight = 270.0 / 720.0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
root = new Group();
root.getChildren().add(stem);
root.getChildren().add(wheel);
wheel.setX(width * distanceWidth - wheel.getImage().getWidth() / 2);
wheel.setY(height * distanceHeight - wheel.getImage().getHeight() / 2);
primaryStage.setTitle("Pinwheel");
primaryStage.setScene(new Scene(root));
primaryStage.show();
primaryStage.getIcons().add(new Image(getClass().getResource("windrad.png").toExternalForm()));
animation().start();
}
private AnimationTimer animation() {
return new AnimationTimer() {
@Override
public void handle(long now) {
wheel.setRotate(wheel.getRotate() + 1);
}
};
}
}
我的意思是在场景图中放置节点,而不是在画布上绘制。当然,您正在(当然)将画布放置在场景图中。但一旦画出画布,就很难修改它@詹姆斯·杜德
这个评论帮助并解决了我的问题。问题中列出了新的源代码。我没有将其绘制到画布上,而是将所有对象放在场景图中,并移动imageview,而不是画布或图像。以下是新的源代码:
package sample;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class Main extends Application {
private Group root;
private final ImageView stem = new ImageView(new Image(getClass().getResource("windrad.png").toExternalForm()));
private final ImageView wheel = new ImageView(new Image(getClass().getResource("rad.png").toExternalForm()));
private final int height = 720;
private final int width = 720;
private final double distanceWidth = 360.0 / 720.0;
private final double distanceHeight = 270.0 / 720.0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
root = new Group();
root.getChildren().add(stem);
root.getChildren().add(wheel);
wheel.setX(width * distanceWidth - wheel.getImage().getWidth() / 2);
wheel.setY(height * distanceHeight - wheel.getImage().getHeight() / 2);
primaryStage.setTitle("Pinwheel");
primaryStage.setScene(new Scene(root));
primaryStage.show();
primaryStage.getIcons().add(new Image(getClass().getResource("windrad.png").toExternalForm()));
animation().start();
}
private AnimationTimer animation() {
return new AnimationTimer() {
@Override
public void handle(long now) {
wheel.setRotate(wheel.getRotate() + 1);
}
};
}
}
你必须使用画布吗?在场景图中放置节点可能更容易。另请参见。请回答您的问题并直接包含代码。@James\u D如何直接包含代码?只需在文本编辑器中复制粘贴或命令?我将阅读ob场景图@詹姆斯:好的,真的很管用!谢谢,问题解决了。太好了。如果你认为这会对他人有所帮助,你可以考虑用自己的问题(用代码等)来回答问题。你可以在宽限期后接受答案。你必须使用画布吗?在场景图中放置节点可能更容易。另请参见。请回答您的问题并直接包含代码。@James\u D如何直接包含代码?只需在文本编辑器中复制粘贴或命令?我将阅读ob场景图@詹姆斯:好的,真的很管用!谢谢,问题解决了。太好了。如果你认为这会对他人有所帮助,你可以考虑用自己的问题(用代码等)来回答问题。你可以在一段宽限期后接受答案。