1

I have the following problem: I have a rectangle, which have a large width. As you see in the Picture (1) the Rectangle width is greater then the pane width. So the part of the rectangle should be invisible or cut of like in picture (2) Picture number 1

Picture number

1 Answers1

0

I didn't understand What you exactly want but I guess this will help

import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TestApp extends Application {

    @Override
    public void start(Stage stage) throws IOException{
       Rectangle rec = new Rectangle(200 , 50);
       rec.setLayoutX(20);
       rec.setFill(Color.BLUE);
       Pane pane = new Pane();
       pane.setPrefSize(200, 50);
       pane.setStyle(" -fx-background-color : red");
       rec.setWidth(pane.getPrefWidth() - rec.getLayoutX());
       rec.layoutXProperty().addListener((observable, oldValue, newValue) -> {
           rec.setWidth(pane.getWidth() - newValue.doubleValue());
       });
       pane.getChildren().add(rec);
       stage.setScene(new Scene(new Pane(pane) , 400 , 100));
       stage.show();
    }

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

I hope that what you wanted And if you care about those colors you can clip the rectangle Have a look at this one explains the clip https://stackoverflow.com/a/15922252/14226680

or the clip in your case is

import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class TestApp extends Application {

    @Override
    public void start(Stage stage) throws IOException{
       Rectangle rec = new Rectangle(22222 , 50);
       rec.setLayoutX(20);
       rec.setFill(Color.BLUE);
       Pane pane = new Pane();
       pane.setPrefSize(500, 50);
       pane.setStyle(" -fx-background-color : red");
       Rectangle clip = new Rectangle(pane.getPrefWidth(), pane.getPrefHeight());
       clip.setLayoutX(pane.getLayoutX());
       clip.setLayoutX(pane.getLayoutY());
       rec.setClip(clip);
       pane.getChildren().add(rec);
       stage.setScene(new Scene(new Pane(pane) , 400 , 100));
       stage.show();
    }

    public static void main(String[] args) throws InterruptedException {
        launch(args);
    }
}
Abood Moorad
  • 31
  • 1
  • 4
  • I'd advise setting the clip on the parent pane, rather than the rectangle and binding the height and width of the rectangle used in the clip to the height and width of the parent pane (perhaps taking into account the padding setting of the parent pane) so that the clip automatically applies for resizing events. That is, if you want to solve this problem for the more general case rather than the specific case of just this rectangle and the initial size of the pane. – jewelsea Nov 08 '21 at 20:46