An Accordion allows a group of TitledPanes (each TitledPane has a Title and a Node (a Polygon here) .
Here we make 10 TitledPanes, using the makeTitledPane method, for polygons from 3 to 12 sides.
package ex80;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
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 Ex80 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;
TitledPane[] titledPaneArray = new TitledPane[N-2];
for (int i = 3; i<=N; i++) {
titledPaneArray[i-3] = makeTitledPane(i);
}
Accordion accordion = new Accordion();
accordion.getPanes().addAll(titledPaneArray);
HBox hbox = new HBox(40, vbox, accordion);
hbox.setAlignment(Pos.CENTER);
hbox.setStyle("-fx-background-color: lime");
Scene scene = new Scene(hbox, 600, 500);
stage.setTitle("Example 80. Accordion");
stage.setScene(scene);
stage.show();
}
private TitledPane makeTitledPane(int num) {
Polygon poly = new Polygon(getDouble(num));
poly.setFill(Color.RED);
return new TitledPane(num + "-sided Polygon", poly);
}
private double[] getDouble (int n) {
double[] array = new double[2*n];
for (int i = 0; i&t;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 with 12-sided polygon selected:
No comments:
Post a Comment