【发布时间】:2020-12-02 13:51:49
【问题描述】:
在下面的示例中,我将一个灰色矩形与高度和宽度的窗格绑定在一起。蓝色多边形应与矩形成比例。这意味着当我调整窗口大小时,矩形和多边形的边框应该具有相同的大小。
我想我可以使用矩形的 .addListener 方法(通过 widthProperty 和 heightProperty)来实现这一点。但我没有成功。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class PolygonFunktion extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Pane pane = new Pane();
//Rectangle
Rectangle rec = new Rectangle();
rec.setFill(Color.GRAY);
rec.layoutXProperty().set(20);
rec.layoutYProperty().set(20);
rec.widthProperty().bind(pane.widthProperty().subtract(rec.layoutXProperty().multiply(2)));
rec.heightProperty().bind(pane.heightProperty().subtract(rec.layoutYProperty().multiply(2)));
//Polygon
Polygon poly = new Polygon();
Double[] xy = {0.,0., 0.,-10., 10.,-10., 10.,-20., 20.,-20., 20.,-30., 30.,-30., 30.,0. };
poly.getPoints().addAll(xy);
poly.setFill(Color.BLUE);
poly.layoutXProperty().bind(rec.layoutXProperty());
poly.layoutYProperty().bind(rec.layoutYProperty().add(rec.heightProperty()));
rec.widthProperty().addListener( (o,oP,nP) -> {
//... ?
});
rec.heightProperty().addListener( (o,oP,nP) -> {
//... ?
});
pane.getChildren().add(rec);
pane.getChildren().add(poly);
stage.setScene(new Scene(pane, 300, 300));
stage.show();
}
}
我不想使用缩放方法,因为多边形的边缘也会被缩放。
【问题讨论】:
-
只需重新计算每个侦听器中的点。
-
非常感谢。很高兴知道我在正确的轨道上。
标签: javafx resize polygon points property-binding