Tuesday, June 21, 2016

60. TilePane

A TilePane organizes nodes in a table kind of view, with the height and width set by the biggest element in a row or column.


Here 12 different sized and colored rectangles are drawn and put in a new slot.


package ex60;

import java.util.ArrayList;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Ex60 extends Application {

    final int NUMCOLORS = 12;
    @Override
    public void start(Stage stage) {
       
        ArrayList<Color> colors = new ArrayList<>();
        
        Random rand = new Random();
        
        int i, r, g, b;
        
        for (i = 0; i<NUMCOLORS; i++) {
            r = rand.nextInt(256);
            b = rand.nextInt(256);
            g = rand.nextInt(256);
            colors.add(Color.rgb(r, g, b));
        }
        
        TilePane tilePane = new TilePane();
        tilePane.setPadding(new Insets(50, 50, 50, 50));
        tilePane.setHgap(25);
        tilePane.setVgap(25);
        
        for (i = 0; i < colors.size(); i++) {
            int w = 50+rand.nextInt(51); // 50 to 100
            int h = 50+rand.nextInt(51); // 50 to 100
            Rectangle rect = new Rectangle(w, h, colors.get(i));
            rect.setArcWidth(25);
            rect.setArcHeight(25);
            tilePane.getChildren().add(rect);
        }

        Scene scene = new Scene(tilePane, Color.GRAY);
        stage.setTitle("Example 60. TilePane");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

This is the output:


No comments:

Post a Comment