我试图以类似矩阵的方式制作矩形列表,我希望它们根据行中矩形的数量进行缩放,以始终适合窗口的固定大小。根据矩形的数量,行尾被小切或其中几个在边界之外。
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
final int size = 80;
stage.setWidth(1040);
stage.setHeight(920);
final double cellDimension = (stage.getWidth() / size) - 1;
stage.setScene(new Scene(render(20,size,cellDimension),Color.WHITE));
stage.show();
}
public static Region render(int generations,int size,double cellDimension) {
VBox results = new VBox(0);
Random random = new Random();
for (int y = 0; y < generations; y++) {
HBox hBox = new HBox(0);
hBox.getchildren().addAll(IntStream.range(0,size).
mapToObj(idx -> random.nextInt(2)).map(item -> createAppropriateRectangle(item == 1,cellDimension)).
collect(Collectors.toList()));
results.getchildren().add(hBox);
}
results.setsnapToPixel(true);
return results;
}
private static Rectangle createAppropriateRectangle(boolean state,double cellDimension) {
Rectangle rectangle = new Rectangle(cellDimension,cellDimension);
rectangle.setStroke(Color.GRAY);
if (state) {
rectangle.setfill(Color.WHITE);
}
return rectangle;
}
}
我做错了什么?如果有什么不清楚的请告诉我,谢谢:) for list of 90 values,up is original,bottom shows how much was outside of border
编辑: 使用@DaveB 剥离示例制作 mwe