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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;
import org.jfree.data.general.DefaultPieDataset;
public class JFrameGraphique extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public JFrameGraphique() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
graphiques = new javax.swing.JPanel();
commandes = new javax.swing.JPanel();
btnTailleVariable = new javax.swing.JButton();
btnTailleFixe = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTabbedPane1.addTab("Utilisateurs", jPanel1);
jTabbedPane1.addTab("Entra\u00eenements", jPanel2);
graphiques.setLayout(new java.awt.BorderLayout());
graphiques.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Graphiques", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 255)));
commandes.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Commandes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 255)));
btnTailleVariable.setText("Graphique taille variable");
btnTailleVariable.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTailleVariableActionPerformed(evt);
}
});
btnTailleFixe.setText("Graphique taille fixe");
btnTailleFixe.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnTailleFixeActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(graphiques, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
.addComponent(commandes, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnTailleVariable, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnTailleFixe, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnTailleFixe, btnTailleVariable});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnTailleFixe)
.addComponent(btnTailleVariable))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(graphiques, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(commandes, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnTailleFixeActionPerformed(java.awt.event.ActionEvent evt) {
// pour remise à zéro du panneau
graphiques.removeAll();
// panneau pour encadrer le graphique :
JPanel cadreIntermédiaire = new JPanel();
// placement du graphique dans le cadre intermédiaire
cadreIntermédiaire.add(prépareGraphique());
// placement du cadre intermédiaire dans le panneau "graphiques"
// le dessin n'est plus déformé par les changements de taille du JFrame car bloqué par le cadre intermédiaire
graphiques.add(cadreIntermédiaire);
graphiques.validate();
}
private void btnTailleVariableActionPerformed(java.awt.event.ActionEvent evt) {
// pour remise à zéro du panneau
graphiques.removeAll();
// ajout simple et direct au panneau "graphiques"
// la taille du graphique va s'adapter automatiquement aux changements de taille du JFrame
graphiques.add(prépareGraphique());
graphiques.validate();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrameGraphique().setVisible(true);
}
});
}
private ChartPanel prépareGraphique(){
// fabrication du graphique (panneau crepart)
DefaultPieDataset union = new DefaultPieDataset();
union.setValue("Pierre",30.0);
union.setValue("Marie",20.0);
union.setValue("Jean",7.0);
union.setValue("Hime",13.0);
union.setValue("Sarah",30.0);
JFreeChart repart =
ChartFactory.createPieChart3D("Répartition par personne",
union,true, true, false);
ChartPanel crepart = new ChartPanel(repart);
Plot plot = repart.getPlot();
return crepart;
}
// Variables declaration - do not modify
private javax.swing.JButton btnTailleFixe;
private javax.swing.JButton btnTailleVariable;
private javax.swing.JPanel commandes;
private javax.swing.JPanel graphiques;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTabbedPane jTabbedPane1;
// End of variables declaration
} |
Partager