对于程序的循环?



所以我正在编写一个关于蒙特卡洛斯数学问题的程序,我正在做它的视觉方面。我基本上已经设置好了所有内容,唯一的问题是由于某种原因我无法将像素放入循环中。有什么帮助吗?不过我已经尝试过 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);
}
}

代码

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 所说,每次通过循环时都应该创建一个新对象,然后将其添加到窗格中。

如果你想要负坐标试试这个

最新更新