Different randomly colored circles, of increasing size, are added in a FlowPane layout.
Circle creation is in the addCirc() method. Circles are added Left to Right and and then Top to Down in default setup.
package ex19;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Ex19 extends Application {
FlowPane flowPane = new FlowPane();
@Override
public void start(Stage primaryStage) {
flowPane.setVgap(10);
flowPane.setHgap(10);
flowPane.setPadding(new Insets(25));
Random random = new Random();
int r, g, b;
for (int i=10; i<80; i+=5) {
r = random.nextInt(256);
g = random.nextInt(256);
b = random.nextInt(256);
addCirc(i, Color.rgb(r, g, b));
}
Scene scene = new Scene(flowPane, 500, 500, Color.HONEYDEW);
primaryStage.setTitle("Example 19. Flowpane");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private void addCirc(double radius, Color color) {
Circle circle = new Circle();
circle.setRadius(radius);
circle.setFill(color);
flowPane.getChildren().add(circle);
}
}
This is the output:
No comments:
Post a Comment