javafx - 调整图像大小以保留纵横比后访问图像的高度和宽度值

问题描述 投票:2回答:1

大家好,感谢您的阅读。我目前正在构建一个图像注释工具。图像被加载到父容器(borderpane)并绑定到父容器(borderpane)。宽高比也得以保留。附带的代码片段说明了这些要点。

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

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

最重要的是,我放置了一个画布,用户可以在其上绘制矩形。绘制矩形后,会弹出一个弹出窗口,可以在其中编写注释。保存后,用户可以将鼠标悬停在绘制的矩形上,并显示注释。

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

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

任何帮助将不胜感激。我打了一堵墙。我甚至不确定我在JavaFX中是否可以提出要求。再次感谢您的阅读。

java javafx imageview aspect-ratio
1个回答
0
投票

晚上好。以下是我为未来可能需要帮助处理类似问题的人设法解决问题的方法。

首先,使用以下代码创建“Resizable Canvas”:

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

有任何问题,请告诉我。

干杯!

© www.soinside.com 2019 - 2024. All rights reserved.