Java: jFreeChart dans un JPanel
Résolu
Remad
Messages postés
1662
Date d'inscription
Statut
Membre
Dernière intervention
-
Remad Messages postés 1662 Date d'inscription Statut Membre Dernière intervention -
Remad Messages postés 1662 Date d'inscription Statut Membre Dernière intervention -
Bonjour à tous,
Je suis en train de produire un programme pour valider mon master qui ressemble à une simulation aérienne, et je dois afficher des graphiques en temps réel sur l'état du joystick et des surfaces de contrôle de l'avion. J'ai cru comprendre que jFreeChart était la meilleur solution pour cela avec la fonction XYSplineRender: https://www.jfree.org/jfreechart/images/XYSplineRendererDemo1a.png (si je me trompe, dites-le moi).
Seulement voila, j'ai commencé à faire mon IHM avec l'interface de NetBeans (qui pour le moment se compose d'une JFrame avec 3 JLabel et 1 JTabbedPane, qui lui même est composé de 3 JLabel) et je ne comprends pas comment faire pour implanter mes graphs dans une des JLabel qui se trouve dans la JTabbedPane. J'ai cru comprendre qu'il fallait implanter les graphs dans un chartPanel, mais je ne vois pas comment le faire comprendre à NetBeans.
Quelqu'un peut m'aider?
Merci d'avance!!
Je suis en train de produire un programme pour valider mon master qui ressemble à une simulation aérienne, et je dois afficher des graphiques en temps réel sur l'état du joystick et des surfaces de contrôle de l'avion. J'ai cru comprendre que jFreeChart était la meilleur solution pour cela avec la fonction XYSplineRender: https://www.jfree.org/jfreechart/images/XYSplineRendererDemo1a.png (si je me trompe, dites-le moi).
Seulement voila, j'ai commencé à faire mon IHM avec l'interface de NetBeans (qui pour le moment se compose d'une JFrame avec 3 JLabel et 1 JTabbedPane, qui lui même est composé de 3 JLabel) et je ne comprends pas comment faire pour implanter mes graphs dans une des JLabel qui se trouve dans la JTabbedPane. J'ai cru comprendre qu'il fallait implanter les graphs dans un chartPanel, mais je ne vois pas comment le faire comprendre à NetBeans.
Quelqu'un peut m'aider?
Merci d'avance!!
A voir également:
- Jfreechart
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
- Waptrick java voiture - Télécharger - Jeux vidéo
6 réponses
Salut!
Je ne vois pas bien ce que tu veux dire par "comment le faire comprendre à NetBeans"... il te suffit de taper le code nécessaire. A savoir un truc du style
;-)
HackTrack
Je ne vois pas bien ce que tu veux dire par "comment le faire comprendre à NetBeans"... il te suffit de taper le code nécessaire. A savoir un truc du style
JFrame frame = new JFrame("Démo"); ... Plot plot = ...//construire l'objet Plot JFreeChart chart = JFreeChart("My chart demo", plot); ChartPanel panel = new ChartPanel(chart); ... frame.getContentPane().add(panel);
;-)
HackTrack
Salut!
Ce n'est qu'un exemple parmi d'autres que je t'ai donné afin de te montrer comment ajouter un ChartPanel à une JFrame
Tu peux aussi créer ton ChartPanel avec comme tu le montres ci-dessus.
L'important est que ChartPanel étend la classe JPanel. Tu peux donc l'ajouter sur le Container d'une JFrame (par exemple) comme je te l'ai montré dans le pseudo-code de mon post précédent.
De quel type de classe est ton objet graphicsPanel? Un JPanel?
;-)
Ce n'est qu'un exemple parmi d'autres que je t'ai donné afin de te montrer comment ajouter un ChartPanel à une JFrame
Tu peux aussi créer ton ChartPanel avec comme tu le montres ci-dessus.
L'important est que ChartPanel étend la classe JPanel. Tu peux donc l'ajouter sur le Container d'une JFrame (par exemple) comme je te l'ai montré dans le pseudo-code de mon post précédent.
De quel type de classe est ton objet graphicsPanel? Un JPanel?
;-)
Oui, j'ai un peu changé le code, j'ai crée une classe graphics qui se présente comme sa pour le moment, elle est inspiré de cet exemple: http://www.java2s.com/Code/Java/Chart/JFreeChartMultipleDatasetDemo1.htm
Et dans le constructeur de l'IHM, j'ai ajouté ces lignes:
Du coup, j'ai plus d'erreur dans l'objet de l'IHM, mais toujours dans la classe Graphics au niveau de
at comet.Graphics.<init>(Graphics.java:46)
at comet.Window.initCharts(Window.java:57)
at comet.Window.<init>(Window.java:51)
at comet.Comet.main(Comet.java:67)
Je commence à être perdu...
package comet; import java.awt.BorderLayout; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class Graphics{ private XYPlot plot; private XYSeriesCollection dataset; private JFreeChart chart; private JPanel content; private ChartPanel chartPanel; public Graphics (final String title) { dataset = createRandomDataset("Serie"); chart = ChartFactory.createXYLineChart("title", "Axis 1", "Axis 2", (XYDataset) dataset, PlotOrientation.VERTICAL, true, true, false); plot = chart.getXYPlot(); content = new JPanel(new BorderLayout()); chartPanel = new ChartPanel(chart); content.add(chartPanel); } private XYSeriesCollection createRandomDataset(String name) { final XYSeries series = new XYSeries(name); double value = 100.0; for (int i = 0; i < 50; i++) { series.add(i, value); value = value * (1.0 + Math.random() / 100); } return new XYSeriesCollection(series); } public JPanel getContent() { return content; } } }
Et dans le constructeur de l'IHM, j'ai ajouté ces lignes:
Graphics test = new Graphics("coucou"); JPanel graphic1 = test.getContent(); joystickChartPanel.add(graphic1);
Du coup, j'ai plus d'erreur dans l'objet de l'IHM, mais toujours dans la classe Graphics au niveau de
content.add(chartPanel);, lorsque je lance le programme: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: javax.swing.JPanel.add
at comet.Graphics.<init>(Graphics.java:46)
at comet.Window.initCharts(Window.java:57)
at comet.Window.<init>(Window.java:51)
at comet.Comet.main(Comet.java:67)
Je commence à être perdu...
Netbeans me sort cette erreur avant la compilation:
Error
no suitable method found for add(org.jfree.chart.ChartPanel)
method java.awt.Container.add(java.awt.Component,java.lang.Object,int) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.awt.Component,java.lang.Object) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.awt.Component,int) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.lang.String,java.awt.Component) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.awt.Component) is not applicable
(actual argument org.jfree.chart.ChartPanel cannot be converted to java.awt.Component by method invocation conversion)
method java.awt.Component.add(java.awt.PopupMenu) is not applicable
(actual argument org.jfree.chart.ChartPanel cannot be converted to java.awt.PopupMenu by method invocation conversion)
Error
no suitable method found for add(org.jfree.chart.ChartPanel)
method java.awt.Container.add(java.awt.Component,java.lang.Object,int) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.awt.Component,java.lang.Object) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.awt.Component,int) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.lang.String,java.awt.Component) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.awt.Component) is not applicable
(actual argument org.jfree.chart.ChartPanel cannot be converted to java.awt.Component by method invocation conversion)
method java.awt.Component.add(java.awt.PopupMenu) is not applicable
(actual argument org.jfree.chart.ChartPanel cannot be converted to java.awt.PopupMenu by method invocation conversion)
Vraiment bizarre car le code suivant tourne sans problème sous Eclipse et ton graphique s'affiche correctement.
Ce ne serait pas un problème d'incompatibilité de version entre la librairie "jfreechart" et la librairie "jcommon"?
;-)
package hacktrack.chartpanel; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class Graphics { private XYPlot plot; private XYSeriesCollection dataset; private JFreeChart chart; private JPanel content; private ChartPanel chartPanel; public Graphics(final String title) { dataset = createRandomDataset("Serie"); chart = ChartFactory.createXYLineChart("title", "Axis 1", "Axis 2", (XYDataset) dataset, PlotOrientation.VERTICAL, true, true, false); plot = chart.getXYPlot(); content = new JPanel(new BorderLayout()); chartPanel = new ChartPanel(chart); content.add(chartPanel); } private XYSeriesCollection createRandomDataset(String name) { final XYSeries series = new XYSeries(name); double value = 100.0; for (int i = 0; i < 50; i++) { series.add(i, value); value = value * (1.0 + Math.random() / 100); } return new XYSeriesCollection(series); } public JPanel getContent() { return content; } public static void main(String[] args) { JFrame frame = new JFrame("Demo ChartPanel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Graphics test = new Graphics("coucou"); JPanel graphic1 = test.getContent(); JPanel joystickChartPanel = new JPanel(); joystickChartPanel.add(graphic1); frame.getContentPane().add(joystickChartPanel); frame.pack(); frame.setVisible(true); } }
Ce ne serait pas un problème d'incompatibilité de version entre la librairie "jfreechart" et la librairie "jcommon"?
;-)
Et avec une Window, ça fonctionne aussi:
public static void main(String[] args) { JFrame frame = new JFrame("Demo ChartPanel"); Window win = new Window(frame); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Graphics test = new Graphics("coucou"); JPanel graphic1 = test.getContent(); JPanel joystickChartPanel = new JPanel(); joystickChartPanel.add(graphic1); win.add(joystickChartPanel); win.pack(); win.setVisible(true); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Je ne comprends plus rien... J'ai simplement copié mon code sur un autre PC, et la, sa fonctionne... A peu près... Je n'ai plus d'erreurs de code, mais les graphiques ne s'affichent pas...
Vous pouvez voir tout les fichiers ici: http://www.tech.dmu.ac.uk/~p09267388/
Vous pouvez voir tout les fichiers ici: http://www.tech.dmu.ac.uk/~p09267388/
Sa fonctionne! Enfin! :D
Aparemment, il falait que je fasse des setLayout des jPanel, que j'ajoute mes ChartPanel et que j'appelle la fonction validate après!
J'ai renommé la classe Graphics en Chart:
Ensuite, le début du constructeur de mon IHM ressmeble à sa:
En tout cas, merci pour ton aide!
Aparemment, il falait que je fasse des setLayout des jPanel, que j'ajoute mes ChartPanel et que j'appelle la fonction validate après!
J'ai renommé la classe Graphics en Chart:
package comet; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; public class Chart{ //private XYPlot plot; private XYSeriesCollection dataset; private JFreeChart chart; private ChartPanel chartPanel; public Chart (final String title) { dataset = createRandomDataset("Serie"); chart = ChartFactory.createXYLineChart("title", "Axis 1", "Axis 2", (XYDataset) dataset, PlotOrientation.VERTICAL, true, true, false); //plot = chart.getXYPlot(); chartPanel = new ChartPanel(chart); } private XYSeriesCollection createRandomDataset(String name) { XYSeries series = new XYSeries(name); double value = 100.0; for (int i = 0; i < 50; i++) { series.add(i, value); value = value * (1.0 + Math.random() / 100); } return new XYSeriesCollection(series); } public ChartPanel getChartPanel() { return chartPanel; } }
Ensuite, le début du constructeur de mon IHM ressmeble à sa:
public class Window extends javax.swing.JFrame { public Window(String name, boolean buttons, int buttonNumber, boolean thumb) { initComponents(); initCharts(); this.setLocationRelativeTo(null); this.name.setText(name); this.buttons.setText(String.valueOf(buttons)); this.buttonsNumber.setText(String.valueOf(buttonNumber)); this.thumb.setText(String.valueOf(thumb)); this.pack(); this.setVisible(true); } private void initCharts() { Chart test1 = new Chart("coucou1"); ChartPanel graphic1 = test1.getChartPanel(); joystickChartPanel.setLayout(new java.awt.BorderLayout()); joystickChartPanel.add(graphic1, BorderLayout.CENTER); joystickChartPanel.validate(); Chart test2 = new Chart("coucou2"); ChartPanel graphic2 = test2.getChartPanel(); fcsChartPanel.setLayout(new java.awt.BorderLayout()); fcsChartPanel.add(graphic2, BorderLayout.CENTER); fcsChartPanel.validate(); Chart test3 = new Chart("coucou3"); ChartPanel graphic3 = test3.getChartPanel(); fmChartPanel.setLayout(new java.awt.BorderLayout()); fmChartPanel.add(graphic3, BorderLayout.CENTER); fmChartPanel.validate(); } //[...] Le code continue
En tout cas, merci pour ton aide!
Pour le code, Qu'est ce que c'est ton objet plot? Sur internet, j'ai cru voir qu'il falait faire comme sa:
Et puis, j'aimerai que ce soit dans un JPanel qui est généré par Netbeans. Quand je veux faire sa:
J'ai une erreur qui dit en gros que je ne peux pas ajouter un ChartPanel dans un JPanel...