【问题标题】:For loop for program?程序的for循环?
【发布时间】:2020-02-05 14:14:49
【问题描述】:

所以我正在编写一个关于蒙特卡洛斯数学问题的程序,并且我正在做它的视觉方面。我大部分都设置好了,唯一的问题是由于某种原因我不能把像素放在一个循环中。有什么帮助吗?不过我已经尝试过 for 循环了。

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) 
    {
        int width = 1;
        int random = ((int)Math.random()*400)+1;

        // Create the Colors
        Color white = Color.web("rgb(255,255,255)");
        Color black = Color.web("rgb(0,0,0)");

        //making the square
        Rectangle square = new Rectangle(115,50,400,400);
        square.setFill(black);
        square.setStroke(white);
        square.setStrokeWidth(width);


        //Drawing the Circle and pixels
          Circle circle = new Circle();
          Circle pixel = new Circle();

          //Setting the properties of the circle 
          circle.setCenterX(315.0f); //moves the cirlce left or right 
          circle.setCenterY(250.0f); //moves the circle up and down
          circle.setRadius(200.0f);
          circle.setFill(black);
          circle.setStroke(white);
          circle.setStrokeWidth(width);

          //setting properties of the pixels
          for(int i = 0; i<1000; i++)
          {
                  pixel.setCenterX((int)(Math.random()*400)+1);
                  pixel.setCenterY((int)(Math.random()*400)+1);
                  pixel.setRadius(1);
                  pixel.setFill(white);

          }


        //Creating a Group object  
          Group Root = new Group(circle);

            Pane pane = new Pane();
            pane.getChildren().addAll(square,circle,pixel);
            Scene scene = new Scene(pane,630,500);
            primaryStage.setScene(scene);
            primaryStage.show();


    }

    public static void main(String[] args)
    {

        launch(args);

    }
}

【问题讨论】:

  • 请显示您尝试实现的循环,否则我们无法帮助您修复该循环。
  • //设置像素属性 for(int i = 0; i
  • 我最初在程序中添加了那个循环,但它不起作用,所以我把它拿出来了。
  • 首先,指定标题。究竟是什么问题?是什么导致了问题?第二,您对我们有什么期望?最后你期待什么?
  • @poisn 问题是当我运行程序时,只弹出一个像素。最终目标是在正方形和圆形内随机弹出多个像素。如您所见,我对 java fx 并不那么精通。我试过运行一个 for 循环,但它仍然只创建一个像素。我也尝试将像素的创建放在 for 循环中,但在 pane.getChildren() 代码行中找不到它。老实说,我没有想法,并且我尝试过进行研究,但是当我尝试将其应用于此时,这些都没有任何实际意义。

标签: java javafx


【解决方案1】:

代码

public class PixelLoop extends Application {

    @Override
    public void start(Stage primaryStage) {
        int width = 1;
        int random = ((int)Math.random()*400)+1;

        // Create the Colors
        Color white = Color.web("rgb(255,255,255)");
        Color black = Color.web("rgb(0,0,0)");

        //making the square
        Rectangle square = new Rectangle(115,50,400,400);
        square.setFill(black);
        square.setStroke(white);
        square.setStrokeWidth(width);


        //Drawing the Circle and pixels
        Circle circle = new Circle();

        //Setting the properties of the circle
        circle.setCenterX(315.0f); //moves the cirlce left or right
        circle.setCenterY(250.0f); //moves the circle up and down
        circle.setRadius(200.0f);
        circle.setFill(black);
        circle.setStroke(white);
        circle.setStrokeWidth(width);


        //Creating a Group object
        Group Root = new Group(circle);

        Pane pane = new Pane();
        pane.getChildren().addAll(square,circle);

        //setting properties of the pixels
        for(int i = 0; i<1000; i++) {

            Circle pixel = new Circle();

            pixel.setCenterX((int)(Math.random()*400)+1);
            pixel.setCenterY((int)(Math.random()*400)+1);
            pixel.setRadius(1);
            pixel.setFill(white);

            pane.getChildren().add(pixel);
        }

        Scene scene = new Scene(pane,630,500);
        primaryStage.setScene(scene);
        primaryStage.show();


    }

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

一开始的问题是你一遍又一遍地覆盖你的对象。这就是为什么只有一个像素的原因。

正如@Tobias Brösamle 所说,您应该在每次通过循环时创建一个新对象,然后将其添加到窗格中。

如果你想要负坐标试试this

【讨论】:

    猜你喜欢
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多