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
  • 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...

  • 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;...

  • 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...

  • (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import...

    (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK"); Button btCancel = new Button("Cancel"); EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { System.out.println("The OK button is clicked"); } }; btOK.setOnAction(handler); btCancel.setOnAction(handler); HBox pane = new HBox(5); pane.getChildren().addAll(btOK, btCancel); Scene...

  • # 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...

  • 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...

  • 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 // // //...

  • 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,...

  • I need to make javafx GUI application called Email that implements a prototype user interface for...

    I need to make javafx GUI application called Email that implements a prototype user interface for composing email message. The application should have labelled text fields for To, cc,bcc ,subject line, one for message body and button lebelled Send. When we click Send button, the program should print contents of all fields to standard output using println() statement. I am attaching photos of Email.java and EmailPane.java, I need to make it as per these classes CylinderSta.. Cylinder java MultiCylind.. ....

  • use this code of converting Km to miles , to create Temperature converter by using java...

    use this code of converting Km to miles , to create Temperature converter by using java FX import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.geometry.Pos; import javafx.geometry.Insets; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.event.EventHandler; import javafx.event.ActionEvent; /** * Kilometer Converter application */ public class KiloConverter extends Application { // Fields private TextField kiloTextField; private Label resultLabel; public static void main(String[] args) { // Launch the application. launch(args); } @Override public void start(Stage primaryStage) { //...

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
ADVERTISEMENT