1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| public final class PieChartBuilder {
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Veuillez patienter...", SwingConstants.CENTER));
PieChartBuilder.initAndShowGUI(panel);
frame.getContentPane().add(panel);
frame.setSize(750,550);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void initAndShowGUI(Container container) {
// This method is invoked on Swing thread
final JFXPanel fxPanel = new JFXPanel();
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
SwingUtilities.invokeLater(()-> {
container.removeAll();
container.add(fxPanel);
container.revalidate();
container.repaint();
});
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.CORAL);
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13), new PieChart.Data("Oranges", 25), new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22), new PieChart.Data("Apples", 30));
final PieChart chart = new PieChart(pieChartData);
chart.setPrefWidth(750);
chart.setPrefHeight(550);
chart.setTitle("Imported Fruits (chart)");
final Label caption = new Label("");
caption.setTextFill(Color.WHITE);
caption.setStyle("-fx-font: 20 arial;");
chart.getData().stream().forEach((data) -> {
data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> {
caption.setTranslateX(e.getSceneX());
caption.setTranslateY(e.getSceneY());
caption.setText(String.valueOf(data.getPieValue()) + "%");
});
});
root.getChildren().addAll(chart, caption);
return (scene);
}
} |
Partager