如何在 JavaFX 中的矩形或圆形内添加图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22848829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:22:18  来源:igfitidea点击:

How do I add an image inside a rectangle or a circle in JavaFX?

javaimagejavafxgeometry

提问by Gregg1989

Suppose we have a rectangle called r

假设我们有一个名为 r 的矩形

Rectangle r = new Rectangle(40, 20);

and an image called image

和一个名为 image 的图像

Image image = new Image("...src for image");

How do I fit the image inside the rectangle? Also, how can I make the image move if the rectangle moves too? How do I do the same thing for a circle? Code examples are greatly appreciated.

如何将图像放入矩形内?另外,如果矩形也移动,我怎样才能使图像移动?我如何为一个圆做同样的事情?非常感谢代码示例。

P.S. Jewelsea, I'm waiting for you, lol!

PS Jewelsea,我在等你,哈哈!

采纳答案by K M Rifat ul alom

if you want to fill Rectangle by image, you can follow this:- in your fxml file add a Circle

如果你想用图像填充矩形,你可以按照这个: - 在你的 fxml 文件中添加一个圆形

<Rectangle fx:id="imgMenuUser" />

And in your Controller

在你的控制器中

@FXML
private Rectangle rectangle;
Image img = new Image("/image/rifat.jpg");
rectangle.setFill(new ImagePattern(img));

回答by James_D

How do I fit the image inside the rectangle?

如何将图像放入矩形内?

Put the shape and the image in a StackPane.

将形状和图像放在 StackPane 中。

Also, how can I make the image move if the rectangle moves too?

另外,如果矩形也移动,我怎样才能使图像移动?

Just move the StackPane.

只需移动 StackPane。

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Point2D;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Pane root = new Pane();

            StackPane imageContainer = new StackPane();
            ImageView image = new ImageView(...);
            imageContainer.getChildren().addAll(new Rectangle(64, 48, Color.CORNFLOWERBLUE), image);
            enableDragging(imageContainer);

            root.getChildren().add(imageContainer);

            Scene scene = new Scene(root,800,600);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void enableDragging(Node node) {
        final ObjectProperty<Point2D> mouseAnchor = new SimpleObjectProperty<>();
        node.setOnMousePressed( event -> mouseAnchor.set(new Point2D(event.getSceneX(), event.getSceneY())));
        node.setOnMouseDragged( event -> {
            double deltaX = event.getSceneX() - mouseAnchor.get().getX();
            double deltaY = event.getSceneY() - mouseAnchor.get().getY();
            node.relocate(node.getLayoutX()+deltaX, node.getLayoutY()+deltaY);
            mouseAnchor.set(new Point2D(event.getSceneX(), event.getSceneY()));;
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}