[英]javafx - access height and width values of an image after it is resized to preserve aspect ratio
大家好,谢谢阅读。 我目前正在构建图像注释工具。 图像被加载并绑定到父容器(边框)。 长宽比也被保留。 随附的代码段说明了这些要点。
imageView.setPreserveRatio(true);
imageView.setSmooth(true);
imageView.fitHeightProperty().bind(heightProperty());
imageView.fitWidthProperty().bind(widthProperty());
效果很好:图像适合窗口并在调整窗口大小时进行调整。
在此之上,我放置了一个画布,用户可以在其上绘制矩形。 在绘制矩形之后,将弹出一个窗口,在其中可以编写注释。 保存后,用户可以将鼠标悬停在绘制的矩形上,然后将显示注释。
我想知道是否有可能在调整图像大小以适合窗口之后以及在调整窗口大小的同时调整图像的大小时访问图像的高度和宽度值。
我需要这样做的原因是,我希望画布能够镜像图像的尺寸,以便注释位于其范围之内。 当前,当调整窗口大小时,由于画布设置为窗口的尺寸而非图像的尺寸,因此注释不会保持“固定”到其区域。
任何帮助将不胜感激。 我碰壁了。 而且甚至不确定我要问的内容是否可以在JavaFX中使用。 再次感谢您的阅读。
晚上都。 这是我设法为将来可能需要解决类似问题的任何人解决问题的方法。
首先,使用以下代码创建“可调整大小的画布”:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
class ResizableCanvas extends Canvas {
public ResizableCanvas() {
// Redraw canvas when size changes.
widthProperty().addListener(evt -> draw());
heightProperty().addListener(evt -> draw());
}
private void draw() {
double width = getWidth();
double height = getHeight();
GraphicsContext gc = getGraphicsContext2D();
gc.clearRect(0, 0, width, height);
gc.setStroke(Color.RED);
gc.strokeLine(0, 0, width, height);
gc.strokeLine(0, height, width, 0);
}
@Override
public boolean isResizable() {
return true;
}
@Override
public double prefWidth(double height) {
return getWidth();
}
@Override
public double prefHeight(double width) {
return getHeight();
}
}
完成后,按以下步骤设置舞台:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ImageViewTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ResizableCanvas rc = new ResizableCanvas();
rc.setOnMouseEntered(e -> {
System.out.println("entered ");
});
Image image = new Image("rushmore.png");
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setSmooth(true);
imageView.setCache(true);
AnchorPane rootAnchorPane = new AnchorPane();
AnchorPane resizableCanvasAnchorPane = new AnchorPane();
rc.widthProperty().bind(resizableCanvasAnchorPane.widthProperty());
rc.heightProperty().bind(resizableCanvasAnchorPane.heightProperty());
imageView.fitWidthProperty().bind(stage.widthProperty());
imageView.fitHeightProperty().bind(stage.heightProperty());
imageView.setPreserveRatio(true);
//here's where the 'magic happens'
resizableCanvasAnchorPane.getChildren().addAll(imageView, rc);
rootAnchorPane.getChildren().addAll(resizableCanvasAnchorPane,rc);
Scene scene = new Scene(rootAnchorPane);
scene.setFill(Color.BLACK);
stage.setTitle("ImageView Test");
stage.setWidth(415);
stage.setHeight(200);
stage.setScene(scene);
stage.show();
}
}
非常感谢Dirk Lemmermann和本教程:
https://www.javacodegeeks.com/2014/04/javafx-tip-1-resizable-canvas.html
希望您能看到画布随图像调整大小。 图像上方有红色准则,并且有一个鼠标事件来说明这一点。 检查IDE的控制台,当鼠标在画布的边界内时,您会看到一个事件被触发。
有任何问题,请告诉我。
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.