[英]Binding Shape and Size of Node to Clipping Mask in JavaFX
Is there a convenient way for a clipping mask to just bind whatever shape/size the target node has? 剪切蒙版是否有一种简便的方法来仅绑定目标节点具有的任何形状/大小? Consider the following Region node:
考虑以下“区域”节点:
import javafx.application.Application;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) throws Exception {
Region content = new Region();
content.setStyle(
"-fx-background-color: #444444;" +
"-fx-background-radius: 50px;" +
"-fx-max-width: 150px;" +
"-fx-max-height: 150px;");
StackPane root = new StackPane(content);
Scene scene = new Scene(root, 300, 300);
stage.setScene(scene);
stage.show();
}
}
How can I have a mask that can automatically adjust its own shape and size accordingly? 我如何拥有可以自动调整其自身形状和尺寸的口罩?
This is how I implement this stuff, but it's too long and inconvenient: 这是我实现这些内容的方法,但是它太长且不方便:
import javafx.application.Application;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Region;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) throws Exception {
Region content1 = new Region();
content1.setStyle(
"-fx-background-color: teal;" +
"-fx-background-radius: 50px;" +
"-fx-pref-width: 150px;" +
"-fx-pref-height: 150px;" +
"-fx-translate-x: 30px;" +
"-fx-translate-y: 30px;");
Rectangle mask = new Rectangle();
Region content2 = new Region() {{
// TODO: Implement better clip mask size and shape handling
mask.widthProperty().bind(widthProperty());
mask.heightProperty().bind(heightProperty());
backgroundProperty().addListener(((observable, oldBackground, newBackground) -> {
for (BackgroundFill backgroundFill : newBackground.getFills()) {
double topLeftHRadius = backgroundFill.getRadii().getTopLeftHorizontalRadius();
double topLeftVRadius = backgroundFill.getRadii().getTopLeftVerticalRadius();
double topRightHRadius = backgroundFill.getRadii().getTopRightHorizontalRadius();
double topRightVRadius = backgroundFill.getRadii().getTopRightVerticalRadius();
double bottomLeftHRadius = backgroundFill.getRadii().getBottomLeftHorizontalRadius();
double bottomLeftVRadius = backgroundFill.getRadii().getBottomLeftVerticalRadius();
double bottomRightHRadius = backgroundFill.getRadii().getBottomRightHorizontalRadius();
double bottomRightVRadius = backgroundFill.getRadii().getBottomRightVerticalRadius();
mask.setArcWidth((topLeftHRadius + topRightHRadius + bottomLeftHRadius + bottomRightHRadius) / 2);
mask.setArcHeight((topLeftVRadius + topRightVRadius + bottomLeftVRadius + bottomRightVRadius) / 2);
}
}));
setClip(mask);
getChildren().add(content1);
}};
content2.setStyle(
"-fx-background-color: cyan;" +
"-fx-background-radius: 50px;" +
"-fx-pref-width: 150px;" +
"-fx-pref-height: 150px;" +
"-fx-translate-x: 70px;" +
"-fx-translate-y: 70px;");
Region root = new Region() {{
getChildren().add(content2);
}};
Scene scene = new Scene(root, 300, 300);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
}
}
I'm looking for a convenient, and better way to do this. 我正在寻找一种方便,更好的方法。 On the other hand, the HTML + CSS overflow property can easily achieve this:
另一方面,HTML + CSS 溢出属性可以轻松实现此目的:
div { position: relative; background-color: cyan; border-radius: 50px; width: 150px; height: 150px; /* clipping property */ overflow: hidden; } div:before { content: ''; position: absolute; top: 30px; left: 30px; background-color: teal; border-radius: inherit; width: 100%; height: 100%; }
<div></div>
Run the code and try to manipulate the size of the window. 运行代码,并尝试操纵窗口的大小。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class ClipApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ImageView imageView = new ImageView("https://hips.hearstapps.com/ghk.h-cdn.co/assets/18/01/2048x1024/landscape-1515004324-boston-terrier.jpg?resize=480:*");
StackPane stackPane = new StackPane(imageView);
Scene scene = new Scene(stackPane);
stage.setScene(scene);
stage.show();
Circle circle = new Circle(60);
circle.centerXProperty().bind(stackPane.widthProperty().divide(2.));
circle.centerYProperty().bind(stackPane.heightProperty().divide(2.));
stackPane.setClip(circle);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.