import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.stage.Stage; import java.util.Scanner; import javafx.scene.paint.Color; import static javafx.application.Application.launch; import static javafx.scene.paint.Color.rgb; /** * This program generates up to 11 random circles * * @author Dave Slemon * @version v100 */ public class RandomCircles extends Application { /** * Start method (use this instead of main). * * @param stage The FX stage to draw on * @throws Exception */ @Override public void start(Stage stage) throws Exception { Group root = new Group(); Scene scene = new Scene(root); Canvas canvas = new Canvas(400, 300); // Set canvas Size in Pixels stage.setTitle("Random Circle Generator"); // Set window title root.getChildren().add(canvas); stage.setScene(scene); GraphicsContext gc = canvas.getGraphicsContext2D(); // YOUR CODE STARTS HERE Scanner scanner = new Scanner(System.in); int count = 1; System.out.println("This program generates random sized circles.\n"); boolean ok = false; do { System.out.print("How many circles should be generated? (between 1-11) "); count = scanner.nextInt(); scanner.nextLine(); // Consume the newline character left in the buffer ok = (count >= 1 && count <= 11); } while (!ok); int num=0; Circle c = new Circle(); do { int xx = getRandomInteger(15,350); int yy = getRandomInteger(15,250); int r = getRandomInteger(0,256); int g = getRandomInteger(0,256); int b = getRandomInteger(0,256); c.setCenter(xx,yy); c.setRadius(getRandomInteger(3,50)); c.setColor( rgb(r,g,b), rgb(r,g,b) ); c.draw(gc); num++; } while (num < count); /* Rectangle r = new Rectangle(); int x = 20; int y = 100; do { r.setTopLeft_x(x+(num * 45)); r.setTopLeft_y(100); r.setLength(20); r.setWidth(40); r.draw(gc); num++; } while (num < count); */ // YOUR CODE STOPS HERE stage.show(); } /** * The actual main method that launches the app. * * @param args unused */ public static void main(String[] args) { launch(args); } /** Provides a random int between minimum and maximum,not including maximum @return minimum <= num < maximum */ public static int getRandomInteger( int minimum, int maximum) { return ((int) (Math.random() * (maximum - minimum))) + minimum; } }