Results 1 to 4 of 4
  1. #1
    thaihuy96 is offline Member
    Join Date
    Jan 2016
    Posts
    1
    Rep Power
    0

    Default how to add group of shapes at random spot on the stage javafx

    Hi, I'm trying to create multiple groups of the same multiple shapes in javafx at random locations, but so far I can only get one group of the shapes to appear randomly. I would like to know how to create multiple of the same group of shapes at random location through loop. Thanks you!
    this is my code
    Java Code:
    package ShapesPKG;
    import java.util.Random;
    import javafx.animation.RotateTransition;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.*;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class DrawingShapes extends Application {
    	  @Override // Override the start method in the Application class
    	  public void start(Stage primaryStage) {
    		// TODO Auto-generated method stub
    		  Random random = new Random();
    		  for (int i = 1; i <= 5; i++) {
    		  int xcenter = random.nextInt(500);
    		  int ycenter = random.nextInt(500);
    		Circle circle = new Circle();
    	    circle.setCenterX(xcenter);
    	    circle.setCenterY(ycenter);
    	    circle.setRadius(100);
    	    circle.setStroke(Color.BLACK);
    	    circle.setStrokeWidth(2.0);
    	    circle.setFill(Color.WHITE);
    	    Ellipse ellipse = new Ellipse();
    	    ellipse.setCenterX(xcenter);
    	    ellipse.setCenterY(ycenter);
    	    ellipse.setRadiusX(50);
    	    ellipse.setRadiusY(75);
    	    ellipse.setStroke(Color.BLACK);
    	    ellipse.setStrokeWidth(2.0);
    	    ellipse.setFill(Color.BLUE);
    	    Ellipse el = new Ellipse();
    	    el.setCenterX(xcenter);
    	    el.setCenterY(ycenter);
    	    el.setRadiusX(50);
    	    el.setRadiusY(75);
    	    el.setStroke(Color.BLACK);
    	    el.setStrokeWidth(2.0);
    	    el.setFill(Color.GREEN);
    	    Ellipse ell = new Ellipse();
    	    ell.setCenterX(xcenter);
    	    ell.setCenterY(ycenter);
    	    ell.setRadiusX(50);
    	    ell.setRadiusY(75);
    	    ell.setStroke(Color.BLACK);
    	    ell.setStrokeWidth(2.0);
    	    ell.setFill(Color.RED);
    	    Polygon polygon = new Polygon();
    	    polygon.getPoints().addAll(new Double[]{
    	        260.0, 250.0,
    	        250.0, 240.0,
    	       240.0,250.0,
    	       250.0, 260.0});
    	       polygon.setFill(Color.WHITE);
    	       polygon.setStroke(Color.BLACK);
    	       polygon.setStrokeWidth(7.0);
    	    RotateTransition rt = new RotateTransition(Duration.millis(3000),ellipse);;
    	    rt.setByAngle(360);
    	    rt.setCycleCount(1000);
    	    rt.setAutoReverse(true);
    	    rt.play();
    	    RotateTransition rota = new RotateTransition(Duration.millis(3000),ell);;
    	    rota.setByAngle(240);
    	    rota.setCycleCount(1000);
    	    rota.setAutoReverse(true);
    	    rota.play();
    	    RotateTransition rotate = new RotateTransition(Duration.millis(3000),el);;
    	    rotate.setByAngle(300);
    	    rotate.setCycleCount(1000);
    	    rotate.setAutoReverse(true);
    	    rotate.play();
    	    RotateTransition rotat = new RotateTransition(Duration.millis(100),polygon);;
    	    rotat.setByAngle(720);
    	    rotat.setCycleCount(1000);
    	    rotat.setAutoReverse(false);
    	    rotat.play();
    	    		Pane pane = new Pane();
    	   pane.getChildren().add(circle);
    	   pane.getChildren().add(ellipse);
    	   pane.getChildren().add(el);
    	   pane.getChildren().add(ell);
    	   pane.getChildren().add(polygon);
    	   Scene scene = new Scene(pane, 500,500);
    	   scene.setFill(Color.BLACK);
    	   primaryStage.setScene(scene);
    	   primaryStage.show();}
    	}
    	  public static void main(String[] args) {
    		    launch(args);
    		  
    		  }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,729
    Rep Power
    29

    Default Re: how to add group of shapes at random spot on the stage javafx

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,913
    Rep Power
    10

    Default Re: how to add group of shapes at random spot on the stage javafx

    It appears you are showing the first group before the others are added. Move lines 86-89 outside the for loop. Move statement 80 before the for loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    265
    Rep Power
    3

    Default Re: how to add group of shapes at random spot on the stage javafx

    Right now you create a bunch of nodes and add them to the pane. If you want to handle the nodes in different groups, you have to instantiate the group, add the nodes to the respective group and then add the group to the pane. It is like creating a menuItem, adding that to a menu and adding the menu to the pane.

    Question: did multiple nodes show up in the same place? How many stages are showing up?

Similar Threads

  1. JavaFX : How to set icon for stage
    By kapilkumar.java in forum JavaFX
    Replies: 2
    Last Post: 08-20-2014, 01:33 AM
  2. Replies: 0
    Last Post: 07-28-2014, 12:32 PM
  3. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-17-2013, 11:10 PM
  4. need help with displaying random shapes and colors
    By JavaGeek90 in forum Suggestions & Feedback
    Replies: 0
    Last Post: 11-23-2010, 09:38 PM
  5. JavaFX Script and JavaFX Mobile
    By levent in forum Java Software
    Replies: 1
    Last Post: 01-27-2010, 05:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •