samedi 23 novembre 2019

Adding randomly generated circles (of uniform size) in a scene to represent stars in JavaFX

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

    }
}



Aucun commentaire:

Enregistrer un commentaire