我正在玩JavaFX并且在类start(Stage theStage)中我有以下代码:
/*... Scene, stage, canvas ...*/
GraphicsContext gc = canvas.getGraphicsContext2D();
Image imgage = new Image("gfx/image.png");
gc.drawImage(image, 256, 256);
如何在不旋转轴的情况下旋转它?我发现并尝试过这个:
gc.save(); // saves the current state on stack, including the current transform
gc.rotate(45);
gc.drawImage(image);
gc.restore();
然而,它也会转换轴,当我使用改变位置x&移动图像时y ..它出错了,因为它也使轴旋转。我一直在考虑使用sin和cos函数重新计算运动,但我确信有这么简单的解决方案。
它可能适用于BufferedImage,但它们使用不同的Graphics类绘制它,我不知道如何使它一起工作。
非常感谢:))
编辑:okey,如何只旋转图像而不需要整个GraphicsContext?
解决: 谢谢大家的帮助:))我已经使用这个>>
解决了它ImageView iv = new ImageView(new Image( "gfx/earth.png"));
iv.setRotate(40);
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
Image rotatedImage = iv.snapshot(params, null);
gc.drawImage(rotatedImage, 0, 0);
答案 0 :(得分:2)
让我们看看我能否理解你的问题......
如何在不旋转整个GraphicsContext的情况下旋转图像?
你不能只使用GraphicsContext来做到这一点。您需要旋转GraphicsContext以使用drawImage API将旋转图像渲染到其上。要防止旋转操作影响其他Canvas绘图操作,您可以在执行旋转之前和之后保存和恢复GraphicsContext(如您在问题中的代码中所显示的那样)。
但是,在不旋转GraphicsContext的情况下实现所需内容的一种方法是将SceneGraph与Canvas结合使用。将图像放在ImageView中,对图像应用旋转变换,对其进行快照以获得旋转的图像,然后将旋转的图像绘制到画布中。
ImageView iv = new ImageView(image);
iv.setRotate(40);
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
Image rotatedImage = iv.snapshot(params, null);
gc.drawImage(rotatedImage, 0, 0);
示例代码:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.*;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/** Rotates and places it in a canvas */
public class RotatedImageInCanvas extends Application {
@Override public void start(Stage stage) {
Image image = new Image(
"http://worldpress.org/images/maps/world_600w.jpg", 350, 0, true, true
);
// creates a canvas on which rotated images are rendered.
Canvas canvas = new Canvas(600, 400);
GraphicsContext gc = canvas.getGraphicsContext2D();
ImageView iv = new ImageView(image);
iv.setRotate(40);
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
Image rotatedImage = iv.snapshot(params, null);
gc.drawImage(rotatedImage, 0, 0);
// supplies a tiled background image on which the canvas is drawn.
StackPane stack = new StackPane();
stack.setMaxSize(canvas.getWidth(), canvas.getHeight());
stack.setStyle("-fx-background-image: url('http://1.bp.blogspot.com/_wV5JMD1OISg/TDYTYxuxR4I/AAAAAAAAvSo/a0zT8nwPV8U/s400/louis-vuitton-nice-beautiful.jpg');");
stack.getChildren().add(
canvas
);
// places a resizable padded frame around the canvas.
StackPane frame = new StackPane();
frame.setPadding(new Insets(20));
frame.getChildren().add(stack);
stage.setScene(new Scene(frame, Color.BURLYWOOD));
stage.show();
}
public static void main(String[] args) { launch(RotatedImageInCanvas.class); }
}
通常情况下,我不建议像上面的示例中那样混合场景图和画布API,而是只编写场景图API或仅编写画布API。
有关仅画布解决方案的示例,请参阅以下答案:
通常,对于大多数操作,我发现使用Scene Graph API比编写Canvas API更简单,并建议使用Scene Graph API而不是Canvas API来完成大多数任务。
答案 1 :(得分:1)
根据另一条评论,你应该像我在我的rotateImage函数中那样添加setViewport和setTransform,如果你想围绕一个点旋转图像而不想改变图像的中心。
site <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L,9L, 10L, 2L), .Label = c("t1", "t10", "t2", "t3", "t4", "t5","t6", "t7", "t8", "t9"), class = "factor"), site = c(1L, 1L,1L, 2L, 2L, 3L, 1L, 3L, 2L, 2L)), .Names = c("name", "site"), row.names = c(NA,10L), class = "data.frame")
library(ape)
t1 <- rtree(10)
t2 <- rtree(10)
order <- cbind(t1$tip.label)
list <- merge(order, site, by.x="V1", by.y="name")
x <- list$site
A <- cbind(t1$tip.label, t1$tip.label)
cophyloplot(t1, t2, assoc = A, show.tip.label = T, space=50, col = x)
答案 2 :(得分:1)
一种相当快捷的方法也是有效的,它是平移和旋转整个GraphicsContext,然后相对于0绘制图像(在这种情况下,旋转是从图像的中间开始)。然后,在绘制图像之后,反转操作。
例如,如果您有一个图像(img),一个GraphicsContext(gc)和该图像的位置(xp,yp),则:
// Translate and rotate.
gc.translate(xp, yp);
gc.rotate(degrees);
// Note how the image is drawn relative to 0. Since the image needs to be
// rotated around the image center, the center is simply half of the image
// dimension.
gc.drawImage( image, -image.getWidth/2.0, -image.getImageHeight/2.0);
// Reverse the translation and rotation once drawn.
gc.rotate(-degrees);
gc.translate(-xp, -yp);