Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Suppose we have a rectangle called r

Rectangle r = new Rectangle(40, 20);

and an image called 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!

like image 506
Gregg1989 Avatar asked Apr 03 '14 20:04

Gregg1989


People also ask

How can we load an image in JavaFX?

You can load an image in JavaFX by instantiating the class named Image of the package javafx. scene. image.

How do I add an image to JavaFX GUI?

Create a FileInputStream representing the image you want to load. Instantiate the Image class bypassing the input stream object created above, as a parameter to its constructor. Instantiate the ImageView class. Set the image to it by passing above the image object as a parameter to the setImage() method.

How do I add an icon to a JavaFX label?

You can use a graphic object as a label using the setGraphic() method of the Label class (inherited from javafx. scene. control. Labeled class).

What is StackPane in JavaFX?

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top. If a border and/or padding have been set, the children will be layed out within those insets.

What is the shape rectangle in JavaFX?

In the JavaFX package, there are several shapes such as Rectangle, circle, line, ellipse, etc. that can be used in applications. In this, we are looking into the shape rectangle in detail. The rectangle is a geometrical figure that contains 4 sides, out of which, the angle between the 2 neighboring sides is 90 0.

How to fill a rectangle with an image?

if you want to fill Rectangle by image, you can follow this:- in your fxml file add a Circle @FXML private Rectangle rectangle; Image img = new Image ("/image/rifat.jpg"); rectangle.setFill (new ImagePattern (img));

How to fill rectangle by Image in FXML?

P.S. Jewelsea, I'm waiting for you, lol! Show activity on this post. if you want to fill Rectangle by image, you can follow this:- in your fxml file add a Circle @FXML private Rectangle rectangle; Image img = new Image ("/image/rifat.jpg"); rectangle.setFill (new ImagePattern (img)); Show activity on this post.

How to display an image inside SVG circle in HTML5?

To display an image inside SVG circle, use the <circle> element and set the clipping path. The <clipPath> element is used to define a clipping path. Image in SVG is set using the <image> element. You can try to run the following code to learn how to display an image inside SVG circle in HTML5


2 Answers

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

<Rectangle fx:id="imgMenuUser" />

And in your Controller

@FXML
private Rectangle rectangle;
Image img = new Image("/image/rifat.jpg");
rectangle.setFill(new ImagePattern(img));
like image 98
K M Rifat ul alom Avatar answered Nov 02 '22 17:11

K M Rifat ul alom


How do I fit the image inside the rectangle?

Put the shape and the image in a StackPane.

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

Just move the 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);
    }
}
like image 21
James_D Avatar answered Nov 02 '22 17:11

James_D