-5

so I am writing a program about the monte carlous math problem and I am doing the visual side of it. I mostly have everything set, the only issue is that I can't put the pixels in a loop for some reason. Any help? I've already tried a for loop though.

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);

    }
}
Zachary
  • 1
  • 2
  • 1
    Please show the loop you tried to implement, otherwise we cannot help you in fixing that loop. – Tobias Brösamle Oct 08 '19 at 09:52
  • //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); } – Zachary Oct 08 '19 at 10:02
  • I originally added that loop to the program but it didn't work so I took it out. – Zachary Oct 08 '19 at 10:03
  • First, specify the title. What exactly is the problem? What causes the problem? Second, what do you expect from us? What are you expecting in the end? – poisn Oct 08 '19 at 10:25
  • @poisn the problem is that when I run the program, only one pixel pops up. The end goal is so that multiple pixels popup randomly within the square and within the circle as well. As you can see I'm not that well versed into java fx. I've tried running a for loop but it still only creates one pixel. I have also tried putting the creation of the pixel in a for loop but then it can't be found in the pane.getChildren() line of code. Honestly I'm out of ideas, and I have tried doing research but none of it makes any real sense when i try to apply it to this. – Zachary Oct 08 '19 at 10:31
  • @poisn Also, I'm not really sure what exactly causes the problem. All I know is that whatever it is, it has to do with the loop itself. – Zachary Oct 08 '19 at 10:34
  • Can you create an discussion where we can look together through the code – poisn Oct 08 '19 at 10:45
  • @poisn, sure. it will take a minute since im new to stack overflow. – Zachary Oct 08 '19 at 10:53
  • You only create one pixel and overwrite its position with every iteration. Instead, create a pixel per iteration and immediately add it to a container (like a panel). – Tobias Brösamle Oct 08 '19 at 10:56
  • @TobiasBrösamle, okay that makes sense but how could I do that? – Zachary Oct 08 '19 at 10:58
  • @poisn I just tried looking around and I'm not sure on how to create a discussion. – Zachary Oct 08 '19 at 11:03
  • Since I do not know GUI programming in Java very well (or, GUI programming at all - who even needs those? :)), I cannot say how the containers available are named, so let's assume there is a Container called `Container`. Then you create that Container before the for loop, move `Circle pixel = new Circle()` inside the loop and, also inside the loop, call something like `container.add(pixel)`. So you only would have to add two lines and move one line. To find the appropriate GUI elements is a task that's up to you, using Google you should find plenty. – Tobias Brösamle Oct 08 '19 at 11:40
  • @TobiasBrösamle thanks that helps a lot. I finally got the loop to get the pixel to appear in a random spot but then it disappears. The plus side is that it keeps appearing in a random spot. Now I just have to get the pixel to stay, and then add a new one. – Zachary Oct 08 '19 at 11:49
  • Possible duplicate of [How to draw 10000 circles in Random locations using JavaFX?](https://stackoverflow.com/questions/58178299/how-to-draw-10000-circles-in-random-locations-using-javafx/58178601#58178601) – Abra Oct 08 '19 at 13:02

1 Answers1

1

Code

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);    }
}

In the beginning there was the problem that you overwrote your object over and over again. That is the reason why there was just one pixel.

As @Tobias Brösamle said, you should create a new object every time you pass through the loop and then add it to the pane.

If you want negativ Coordinates try this

poisn
  • 437
  • 2
  • 17