【问题标题】:How to make pyramid patterns of Circle shape using GridPane in javafx如何在 javafx 中使用 GridPane 制作圆形的金字塔图案
【发布时间】:2020-09-01 13:25:47
【问题描述】:

我想创建金字塔左、右、中心角,在 GridPane 中使用圆形,我已经完成了左角,但花了 2 天时间创建右角和中心角,但没有运气。

如果有人知道或能给我一些算法想法,我将不胜感激,请帮助我!

输出已经完成左角

使用以下代码

        int i, j;
        for (i = 0; i < n; i++) {
            for (j = 0; j <= i; j++) {
                Circle circle = new Circle();
                circle.setStroke(Paint.valueOf(Color.BLACK.toString()));
                circle.radiusProperty().bind(ballsModel.radiusProperty());
                circle.strokeWidthProperty().bind(ballsModel.strokeProperty());
                circle.fillProperty().bind(Bindings.createObjectBinding(() -> Paint.valueOf(ballsModel.getColor().name())));
                grid.addRow(i, circle);
            }
        }

需要弄清楚以下模式:

中心角

直角

【问题讨论】:

  • 提示:计算放置圆圈的索引;不要尝试使用addRowaddColumn,而是add(Node, int column, int row)addColumn/addRow 不适用于添加孩子,如果有“单元格”的索引较小保持为空,例如最后一张图片中 (1, 3) 处的 cricle;顺便说一句,我不会为你解决你的家庭作业。不过,找出正确的索引范围应该不会太难。
  • @fabian 这就是我两天以来一直在尝试做的事情,但我需要一些插图代码来完成
  • 也许尝试为三角形的边创建不等式会有所帮助? x &gt;= 0y &lt;= 4y &gt;= x 用于解决问题;这意味着y &gt;= 0(结合第一个和最后一个等式,以防不明显),因此您可以创建一个外循环for (int y = 0; y &lt;= 4; ++y) { ... };现在给定 y,您可以得出 x 的有效范围,将 x &gt;= 0y &gt;= x 组合为 0 &lt;= x &lt;= y 导致 for (int x = 0; x &lt;= y; ++x) { ... } 的内部循环对于提出不等式,two points form 应该有所帮助。
  • 另外,至少对于最后一个形状,您可以认为这是在穿过中心的垂直线上镜像的第一个形状,即点可以转换为 (x, y) -&gt; (k-x, y) 以获得合适的常数 k ...第二个形状可以被认为是最后一个形状和第一个稍微小一点的版本向下和向右移动的组合,但是如果您可以根据 y 确定 x 的上限或下限,那么您已经您也许可以根据对称性确定另一个界限...

标签: javafx geometry gridpane


【解决方案1】:

金字塔部分的例子

package pyramid;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;


public class Pyramid extends Application {



   void createPyramid(){
   gridPane.setHgap(5);
   gridPane.setVgap(5);
   int center = 4 ;


   for (int row = 0; row <= 4; row++ ) {

      int  range = (1 +(2* row));

      int  startColumn = center-range/2 ;

       for(int i = 0 ; i<range; i++){
           Circle circle  = new Circle(20,javafx.scene.paint.Color.BLUE);
           gridPane.add(circle,startColumn+i , row);

       }     

   }} 

@Override
public void start(Stage primaryStage) {


    StackPane root = new StackPane();
    root.getChildren().add(gridPane);
    this.createPyramid();

    Scene scene = new Scene(root, 600, 600);

    primaryStage.setTitle("Pyramid");
    primaryStage.setScene(scene);
    primaryStage.show();
}


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

【讨论】:

  • 太棒了!非常感谢哥们,它解决了我的问题,但是还有1个我无法摆脱正确的,我今天晚上必须提交这个作业,非常感谢你的帮助,现在只需要专注于正确的角度.
  • a GridPane 就像一个二维数组,第 0 行中第一个圆的位置是第 4 列。在每次迭代中,圆圈的数量增加一
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
相关资源
最近更新 更多