Wednesday, June 29, 2016

91. TabPane

We create 10 TabPanes, for poly-3 to poly-12.


Each tab has a node content. Instead of having the Polygon as the content, we put the Polygon inside a VBox and then set the content as that VBox. This way we have greater control of layout parameters, in this case we can center the Polygon, rather than touching the tab labels on top.


package ex91;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Ex91 extends Application {

    final int RADIUS = 100;
    
    @Override
    public void start(Stage stage) {
        
        Label label = new Label("Polygon Shapes");
        label.setFont(Font.font("Georgia", 20));

        VBox vbox = new VBox(label);
        vbox.setAlignment(Pos.CENTER);
        
        int N = 12;
        Tab[] tabPaneArray = new Tab[N-2];
        for (int i = 3; i<=N; i++) {
            tabPaneArray[i-3] = makeTab(i);
        }
        
        TabPane tabPane = new TabPane();
        tabPane.getTabs().addAll(tabPaneArray);
        
        HBox hbox = new HBox(40, vbox, tabPane);
        hbox.setPadding(new Insets(20));
        hbox.setAlignment(Pos.CENTER);
        hbox.setStyle("-fx-background-color: lime");

        Scene scene = new Scene(hbox, 800, 500);
        stage.setTitle("Example 91. TabPane");
        
        stage.setScene(scene);
        stage.show();
    }
    
    private Tab makeTab(int num) {
        Tab tab = new Tab(num + "-poly");
        Polygon poly = new Polygon(getDouble(num));
        poly.setFill(Color.RED);
        VBox vTabBox = new VBox(poly);
        vTabBox.setAlignment(Pos.CENTER);
        tab.setContent(vTabBox);
        tab.setClosable(false);
        return tab;
    }
    
    private double[] getDouble (int n) {
        double[] array = new double[2*n];
        for (int i = 0; i<n; i++){
            array[2*i] = RADIUS*Math.cos(2*Math.PI/n*i);
            array[2*i+1]=RADIUS*Math.sin(2*Math.PI/n*i);
        }
        return array;
    }

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

This is the output when 10-poly is selected:


No comments:

Post a Comment