1 Replies - 1897 Views - Last Post: 23 January 2016 - 12:28 AM Rate Topic: -----

#1 rinx241   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-January 16

how to duplicate javafx shapes randomly?

Posted 22 January 2016 - 10:58 PM

Hi, I'm new to java and javafx in general. I'm trying to generate multiple of a group of shapes randomly on a scene using loop but I cannot figure how to do it, does anyone have suggestion. Thanks you!
this is my code
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
		Circle circle = new Circle();
	    circle.setCenterX(250);
	    circle.setCenterY(250);
	    circle.setRadius(100);
	    circle.setStroke(Color.BLACK);
	    circle.setStrokeWidth(2.0);
	    circle.setFill(Color.WHITE);
	    Ellipse ellipse = new Ellipse();
	    ellipse.setCenterX(250);
	    ellipse.setCenterY(250);
	    ellipse.setRadiusX(50);
	    ellipse.setRadiusY(75);
	    ellipse.setStroke(Color.BLACK);
	    ellipse.setStrokeWidth(2.0);
	    ellipse.setFill(Color.BLUE);
	    Ellipse el = new Ellipse();
	    el.setCenterX(250);
	    el.setCenterY(250);
	    el.setRadiusX(50);
	    el.setRadiusY(75);
	    el.setStroke(Color.BLACK);
	    el.setStrokeWidth(2.0);
	    el.setFill(Color.GREEN);
	    Ellipse ell = new Ellipse();
	    ell.setCenterX(250);
	    ell.setCenterY(250);
	    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);
		  
		  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to duplicate javafx shapes randomly?

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: how to duplicate javafx shapes randomly?

Posted 23 January 2016 - 12:28 AM

Class Random

You wouldn't do drawing in a loop, you need a timer. I searched and found javafx AnimationTimer, there may be others.

Basically, for each timer interval you paint, or cause a paint event, to redraw shapes.

But, if you are new to Java, you should study all the fundamentals of the language first, including loops, events, classes (OOP), etc..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1