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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| package test;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.AnchorPane;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
/**
*
* @author fabriceb
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
}
/**
* Called on EDT.
*/
private static void initAndShowGUI() {
final JButton chartButton = new JButton("Show Chart !");
final JPanel buttonBox = new JPanel();
buttonBox.setBorder(new EmptyBorder(6, 6, 6, 6));
buttonBox.setLayout(new BoxLayout(buttonBox, SwingConstants.HORIZONTAL));
buttonBox.add(chartButton);
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(buttonBox, BorderLayout.SOUTH);
frame.setSize(500, 500);
frame.setVisible(true);
//
chartButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Already there.
for (final Component component : frame.getContentPane().getComponents()) {
if (component instanceof JFXPanel) {
return;
}
}
// Add new chart.
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel, BorderLayout.CENTER);
Platform.runLater(new Runnable() {
@Override
public void run() {
loadChartContent(fxPanel);
}
});
}
});
}
/**
* Called on JavaFX app thread.
* @param fxPanel
*/
private static void loadChartContent(final JFXPanel fxPanel) {
int power = 2;
int minX = 0;
int maxX = 10;
double maxY = 0;
final LineChart.Series series = new LineChart.Series();
series.setName("^ 2");
for (int x = minX; x <= maxX; x++) {
final double y = Math.pow(x, power);
maxY = Math.max(maxY, y);
final LineChart.Data data = new LineChart.Data<>(x, y);
series.getData().add(data);
}
final NumberAxis xAxis = new NumberAxis(minX, maxX, 5);
xAxis.setLabel("x");
final NumberAxis yAxis = new NumberAxis(0, maxY, maxY / 5);
yAxis.setLabel("y");
final LineChart chart = new LineChart(xAxis, yAxis);
chart.getData().add(series);
chart.setTitle("y = x ^ 2");
AnchorPane.setTopAnchor(chart, 0d);
AnchorPane.setLeftAnchor(chart, 0d);
AnchorPane.setBottomAnchor(chart, 0d);
AnchorPane.setRightAnchor(chart, 0d);
final AnchorPane root = new AnchorPane();
root.getChildren().add(chart);
final Scene scene = new Scene(root);
fxPanel.setScene(scene);
}
} |
Partager