Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javafx—在调整图像大小以保持纵横比后,访问图像的高度和宽度值_Java_Javafx_Imageview_Aspect Ratio - Fatal编程技术网

javafx—在调整图像大小以保持纵横比后,访问图像的高度和宽度值

javafx—在调整图像大小以保持纵横比后,访问图像的高度和宽度值,java,javafx,imageview,aspect-ratio,Java,Javafx,Imageview,Aspect Ratio,大家好,谢谢阅读。我目前正在构建一个图像注释工具。图像将加载并绑定到父容器(borderpane)。宽高比也被保留。所附的代码片段说明了这些要点 imageView.setPreserveRatio(true); imageView.setSmooth(true); imageView.fitHeightProperty().bind(heightProperty()); imageView.fitWidthProperty().bind(widthPro

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

     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和本教程:

希望您可以看到画布随图像调整大小。图像上方有红色的指导线和一个鼠标事件来说明这一点。检查IDE的控制台,您可以看到当鼠标位于画布范围内时触发的事件

有任何问题,请告诉我

干杯