0

Since I had such great success getting help with my hi-lo guessing game and figuring out the issues I had. I thought I would post here again to see if I could get some help on figuring out how to get the random generator to work. I am trying to add circles (dots) in the scene I have created with spaceships. They should be randomly placed on the scene but be of a uniform size. I created the random generator but every time I run the code, no stars appear. I'm not sure what I've done wrong here. I'm very new to programming and this is only my second post here on this forum. Any help in troubleshooting would be appreciated.

I only need help in figuring out the last bit of code for the random generator - everything else is working as expected - I figured including all of the code might be helpful to see if there is better placement of the random generator within the code.

public class Spaceship extends Group
{

/*
 Create the elements of the spaceship with its position in one class .
*/

    public Spaceship()
    {
        Ellipse spaceshipbottom = new Ellipse(33.0, 67.0, 59.0, 50.0);
        spaceshipbottom.setStroke(Color.GREY);
        spaceshipbottom.setStrokeWidth(1);
        spaceshipbottom.setFill (Color.YELLOW);

        Ellipse spaceshipbody = new Ellipse(31.0, 72.0, 100.0, 31.0);
        spaceshipbody.setStroke(Color.AQUA);
        spaceshipbody.setStrokeWidth(3);
        spaceshipbody.setFill (Color.DODGERBLUE);

        Ellipse spaceshiptop = new Ellipse(31.0, 44.0, 72.0, 31.0);
        spaceshiptop.setStroke(Color.BLACK);
        spaceshiptop.setStrokeWidth(2);
        spaceshiptop.setFill (Color.DODGERBLUE);

        Ellipse spaceshipcommand = new Ellipse(32.0, 15.0, 54.0, 42.0);
        spaceshipcommand.setStroke(Color.AQUA);
        spaceshipcommand.setStrokeWidth(2);
        spaceshipcommand.setFill (Color.SLATEGRAY);

        Ellipse spaceshipwindow1 = new Ellipse(40.0, 88.0, 10.0, 8.0);
        spaceshipwindow1.setStroke(Color.BLACK);
        spaceshipwindow1.setStrokeWidth(2);
        spaceshipwindow1.setFill (Color.YELLOW);

        Ellipse spaceshipwindow2 = new Ellipse(3.0, 85.0, 10.0, 8.0);
        spaceshipwindow2.setStroke(Color.BLACK);
        spaceshipwindow2.setStrokeWidth(2);
        spaceshipwindow2.setFill (Color.YELLOW);

        Ellipse spaceshipwindow3 = new Ellipse(75.0, 83.0, 10.0, 8.0);
        spaceshipwindow3.setStroke(Color.BLACK);
        spaceshipwindow3.setStrokeWidth(2);
        spaceshipwindow3.setFill (Color.YELLOW);

        Ellipse spaceshipwindow4 = new Ellipse(108.0, 77.0, 10.0, 8.0);
        spaceshipwindow4.setStroke(Color.BLACK);
        spaceshipwindow4.setStrokeWidth(2);
        spaceshipwindow4.setFill (Color.YELLOW);

        Ellipse spaceshipwindow5 = new Ellipse(-40.0, 77.0, 10.0, 8.0);
        spaceshipwindow5.setStroke(Color.BLACK);
        spaceshipwindow5.setStrokeWidth(2);
        spaceshipwindow5.setFill (Color.YELLOW);

        getChildren().addAll(spaceshipbottom,spaceshipbody,spaceshiptop,
                spaceshipcommand,spaceshipwindow1,spaceshipwindow2,
                spaceshipwindow3,spaceshipwindow4,spaceshipwindow5);
    }
}
    @Override
    public void start(Stage primaryStage) {

 /* Make four versions of the spaceship in different sizes and
    positions on the sene.   
        */

       Spaceship spaceship1 = new Spaceship();
       spaceship1.setTranslateX(100);
       spaceship1.setTranslateY(125);

       Spaceship spaceship2 = new Spaceship();
       spaceship2.setTranslateX(350);
       spaceship2.setTranslateY(350);
       spaceship2.setScaleX(1.75);
       spaceship2.setScaleY(1.75);

       Spaceship spaceship3 = new Spaceship();
       spaceship3.setTranslateX(640);
       spaceship3.setTranslateY(150);
       spaceship3.setScaleX(.75);
       spaceship3.setScaleY(.75);

       Spaceship spaceship4 = new Spaceship();
       spaceship4.setTranslateX(370);
       spaceship4.setTranslateY(70);
       spaceship4.setScaleX(.60);
       spaceship4.setScaleY(.60);

// Create background of stars
    Random rand = new Random();
    Group root = new Group();
    StackPane stack = new StackPane();
    Group stars = new Group();
    for (int i = 0; i < 100; i++) {
        int centerx = rand.nextInt();
        int centery = rand.nextInt();
        int radius = rand.nextInt(20);

        Circle circle = new Circle(centerx, centery, radius);
        circle.setStrokeType(StrokeType.OUTSIDE);
        circle.setFill(Color.WHITE);
        circle.setStroke(Color.WHITE);
        circle.setStrokeWidth(1);

    }


       root.getChildren().addAll(spaceship1,spaceship2,spaceship3,spaceship4,stars);


        Scene scene = new Scene(root, 800, 600,Color.BLACK);

        primaryStage.setTitle("Spaceships in Outerspace!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    }
}
  • 1
    Please post [mre]. The posted code is quiet long for demonstrating the problem, yet it is incomplete. – c0der Nov 24 '19 at 07:28
  • 2
    See an [example](https://stackoverflow.com/a/54604889/3992939) of adding shapes randomly. – c0der Nov 24 '19 at 07:40
  • In the loop that creates the `Circles`, you never add the `Circles` to the circle `Group`. Add `stars.getChildren().add(circle);` after `circle.setStrokeWidth(1);`. I would also limit my random numbers where`x: 0 to 800 and y: 0 to 600`. – SedJ601 Nov 24 '19 at 08:18
  • 2
    Let's be generous an assume that there are `2^14 = 16.384‬` possible integral values for `centerX` and `centerY` positions, where the circle could be seen on screen. There are `2^32` (approx 4E9) possible ints produced by `Random.nextInt()`. This means the chances of getting the circle into view horizontally/vertically are `2^14 / 2^32 = 1 / 2^18`. Take the square to calculate the probability of getting a circle into view: `1 / 2^36`. You'd need to be incredibly lucky to get a single circle shown (even if you add them)... – fabian Nov 24 '19 at 08:36

1 Answers1

3

The reason of not showing star is that stars are not added to 'stars''group'. Adding stars.getChildren().add(circle)' to the end of loop is a way to solve the problem.

I have two suggestions to improve your code: 1. Add the 'star''group' to the root first which allows the spaceships to to appear closer in z-order. 2. Set the random generator to return x values from 0 to 799 and y values from 0 to 599. Anything out of random generator's ranges will be off the screen view.

Main Code:

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

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

    @Override
    public void start(Stage stage)
    {
        Spaceship spaceship1 = new Spaceship();
        spaceship1.setTranslateX(100);
        spaceship1.setTranslateY(125);

        Spaceship spaceship2 = new Spaceship();
        spaceship2.setTranslateX(350);
        spaceship2.setTranslateY(350);
        spaceship2.setScaleX(1.75);
        spaceship2.setScaleY(1.75);

        Spaceship spaceship3 = new Spaceship();
        spaceship3.setTranslateX(640);
        spaceship3.setTranslateY(150);
        spaceship3.setScaleX(.75);
        spaceship3.setScaleY(.75);

        Spaceship spaceship4 = new Spaceship();
        spaceship4.setTranslateX(370);
        spaceship4.setTranslateY(70);
        spaceship4.setScaleX(.60);
        spaceship4.setScaleY(.60);

        // Create background of stars
        Random rand = new Random();
        Group root = new Group();

        Group stars = new Group();
        for (int i = 0; i < 100; i++) {
            int centerx = rand.nextInt(800);
            int centery = rand.nextInt(600);
            int radius = rand.nextInt(20);

            Circle circle = new Circle(centerx, centery, radius);
            circle.setStrokeType(StrokeType.OUTSIDE);
            circle.setFill(Color.WHITE);
            circle.setStroke(Color.WHITE);
            circle.setStrokeWidth(1);        

            stars.getChildren().add(circle);
        }

        root.getChildren().addAll(stars, spaceship1,spaceship2,spaceship3,spaceship4);
        Scene scene = new Scene(root, 800, 600,Color.BLACK);
        stage.setScene(scene);
        stage.show();
    }
}

enter image description here

SedJ601
  • 12,173
  • 3
  • 41
  • 59