Question

Write a JavaFX application that displays 10,000 very small circles (radius of 1 pixel) in random...

Write a JavaFX application that displays 10,000 very small circles (radius of 1 pixel) in random locations within the visible area. Fill the dots on the left half of the scene red and the dots on the right half of the scene green. Use the getWidth method of the scene to help determine the halfway point.

This is what I have so far:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.Random;
import javafx.scene.Group;

public class Class615 extends Application
{

@Override
public void start(Stage primaryStage)
{
Group root = new Group();
Random gen = new Random();
Color circleColor = Color.RED;
Color fill = null;
final int radius = 1;

for (int count = 1; count <= 10000; count++)
{
double x = gen.nextInt(1) + 2;
double y = gen.nextInt(1) + 2;
  
Circle circle = new Circle(x, y, radius);
circle.setStroke(Color.WHITE);
circle.setFill(fill);
circle.getCenterX();
double xLocate = circle.getCenterX();
root.getChildren().add(circle);
  
if (xLocate < 150)
{
fill = circleColor;
} else
{
fill = Color.GREEN;
}
  
}

Scene scene = new Scene(root, 300, 250, Color.BLACK);

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

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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Class615.java

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.shape.Circle;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

import java.util.Random;

import javafx.scene.Group;

public class Class615 extends Application {

    @Override

    public void start(Stage primaryStage) {

        Group root = new Group();

        Random gen = new Random();

        final int radius = 1;

        //creating a Scene

        Scene scene = new Scene(root, 300, 250, Color.BLACK);

        //looping for 10000 times

        for (int count = 1; count <= 10000; count++) {

            //generating a random x coordinate between 0 and scene.getWidth()

            double x = gen.nextInt((int) scene.getWidth());

            //generating a random y coordinate between 0 and scene.getHeight()

            double y = gen.nextInt((int) scene.getHeight());

            //creating a Circle at this position

            Circle circle = new Circle(x, y, radius);

            //using no stroke

            circle.setStroke(null);

            //checking if x is on left half or right half

            if (x < scene.getWidth() / 2) {

                //left half, using red as fill color

                circle.setFill(Color.RED);

            } else {

                //using green as fill color

                circle.setFill(Color.GREEN);

            }

            //adding circle to the scene

            root.getChildren().add(circle);

        }

        primaryStage.setTitle("Circles");

        primaryStage.setScene(scene);

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

/*OUTPUT*/


Add a comment
Know the answer?
Add Answer to:
Write a JavaFX application that displays 10,000 very small circles (radius of 1 pixel) in random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //No JPanel please: Write a javafx application that displays an image and //plays a sound effect...

    //No JPanel please: Write a javafx application that displays an image and //plays a sound effect with each mouse click. Rotate through four images and //five sound effects, so the image/sound effect pairing is different each time. //What my problem is on is actually getting the pictures to switch. The x variable //works perfect for the sound change but I am unsure what to do with y for the //image change. Also I dont know if I should put this...

  • WRITE A JavaFX APPLICATION THAT DRAWS A PICTURE OF A FLOWER. Use thick lines for stalks...

    WRITE A JavaFX APPLICATION THAT DRAWS A PICTURE OF A FLOWER. Use thick lines for stalks and rotated ellipses for petals. Put the sky and the ground as background. Don’t worry artistic quality, it should look simple. It should take about 45 to 75 statements. Also, have some line-by-line documentation that says what is going on, e.g., // draw petal // draw ground // draw leaf The snowman program should be used as a GUIDE. Snowman code : import javafx.application.Application;...

  • JavaFX programming (Not Javascript) Using the code from BallPane.java and BounceBallControl.java create a "Pong for One"...

    JavaFX programming (Not Javascript) Using the code from BallPane.java and BounceBallControl.java create a "Pong for One" game - a rectangular paddle moves back and forth via mouse drag along the bottom of the pane; the bottom of the paddle should be about 1/2 the diameter of the ball off the bottom. If the ball connects with the paddle, then it bounces at a 90 degree angle back into the pane space. If the ball misses the paddle, then the score...

  • JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. T...

    JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. The timer should show "count: the beginning and increase the number by 1 in every one second, and so on. o" at .3 A Timer - × A Timer Count: 0 Count: 1 (B) After 1 second, the GUI Window (A) Initial GUI Window According to the...

  • changes needed is at the end of the code //************************************************************************ // Snowman.java Author: Lewis/Loftus // //...

    changes needed is at the end of the code //************************************************************************ // Snowman.java Author: Lewis/Loftus // // Demonstrates the translation of a set of shapes. //************************************************************************ import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.*; public class Snowman extends Application { //-------------------------------------------------------------------- // Presents a snowman scene. //-------------------------------------------------------------------- public void start(Stage primaryStage) { Ellipse base = new Ellipse(80, 210, 80, 60); base.setFill(Color.WHITE);    Ellipse middle = new Ellipse(80, 130, 50, 40); middle.setFill(Color.WHITE);    Circle head = new Circle(80,...

  • 15.3 (Move the ball) Write a program that moves the ball in a pane. You should...

    15.3 (Move the ball) Write a program that moves the ball in a pane. You should define a pane class for displaying the ball and provide the methods for moving the ball left, right, up, and down, as shown in Figure 15.24c. Check the boundary to prevent the ball from moving out of sight completely. import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class Driver extends Application...

  • The JavaFX framework provides more than one way to handle events. For event handlers, we could...

    The JavaFX framework provides more than one way to handle events. For event handlers, we could use inner classes, anonymous inner classes, or the new Java 8 feature of lambda expressions. In this discussion, you will explore these different ways of writing event handles in JavaFX. To prepare for this discussion, you must unzip the attached NetBeans project zip file (U4D1_HandleEvents.zip) and load it into your NetBeans IDE. The project uses an inner class to handle the click event on...

  • Can someone please comment this on what the javafx do and how they are implemented? import...

    Can someone please comment this on what the javafx do and how they are implemented? import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Paint; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class TextPanel extends Application { GridPane grid = null; Text scenetitle = null; Label tDrawLabel = null; Label bsc345Label = null; HBox THBox = null; VBox mVBox = new VBox(2); Stage primaryStage = null; @Override public void start(Stage...

  • # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {...

    # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {   @Override // Override the start method in the Application class   public void start(Stage primaryStage) {     // Create a button and place it in the scene     Button btOK = new Button("OK");     btOK.setOnAction(e -> System.out.println("OK 1"));     btOK.setOnAction(e -> System.out.println("OK 2"));     Scene scene = new Scene(btOK, 200, 250);     primaryStage.setTitle("MyJavaFX"); // Set the stage title     primaryStage.setScene(scene); // Place the scene in the stage     primaryStage.show(); // Display the stage...

  • Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets;...

    Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class NtoThePowerOfN extends Application{ @Override public void start(Stage primaryStage) throws Exception { TextField inputField; TextField outputField; VBox base = new VBox(10); base.setPadding(new Insets(10)); base.setAlignment(Pos.CENTER); // // A: input components - label and text field // // // B: button - to compute the value // // //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT