【问题标题】:javafx - access height and width values of an image after it is resized to preserve aspect ratiojavafx - 在调整图像大小以保持纵横比后访问图像的高度和宽度值
【发布时间】:2019-03-16 15:01:39
【问题描述】:

大家好,感谢阅读。我目前正在构建一个图像注释工具。图像被加载并绑定到父容器(边框)。纵横比也被保留。随附的代码 sn-p 说明了这些要点。

     imageView.setPreserveRatio(true);
     imageView.setSmooth(true);
     imageView.fitHeightProperty().bind(heightProperty());
     imageView.fitWidthProperty().bind(widthProperty());

这很好用:图像适合窗口并在调整窗口大小时调整大小。

在此之上,我放置了一个用户可以在其上绘制矩形的画布。在他们绘制了一个矩形后,将启动一个弹出窗口,他们可以在其中编写注释。保存后,用户可以将鼠标悬停在绘制的矩形上,注释将出现。

我想知道是否可以在调整图像大小以适合窗口后以及在调整窗口大小时调整图像大小时访问图像的高度和宽度值。

我需要这个的原因是我希望画布能够镜像图像的尺寸,以便注释在其范围内。当前,当调整窗口大小时,注释不会保持“固定”到它们的区域,因为画布设置为窗口的尺寸而不是图像的尺寸。

任何帮助将不胜感激。我碰了一点墙。甚至不确定我所问的是否可以在 JavaFX 中实现。再次感谢您的阅读。

【问题讨论】:

    标签: java javafx imageview aspect-ratio


    【解决方案1】:

    晚上好。以下是我为将来可能需要帮助解决类似问题的任何人解决问题的方法。

    首先,使用以下代码创建一个“可调整大小的画布”:

    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 的控制台,您可以看到当鼠标在画布范围内时触发的事件。

    有任何问题,请告诉我。

    干杯!

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 2021-02-09
      • 2013-07-29
      • 2013-04-03
      • 2013-06-26
      • 2012-04-15
      相关资源
      最近更新 更多