Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Balo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.io.File; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author NguyenThanh */ public class Main extends javax.swing.JFrame { /** * Creates new form Main */ public Main() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabelTitle = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); aaa = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabelTitle.setText("Greedy and Dynamic"); jButton1.setText("Show chart"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); aaa.addAncestorListener(new javax.swing.event.AncestorListener() { public void ancestorMoved(javax.swing.event.AncestorEvent evt) { } public void ancestorAdded(javax.swing.event.AncestorEvent evt) { aaaAncestorAdded(evt); } public void ancestorRemoved(javax.swing.event.AncestorEvent evt) { } }); aaa.setLayout(new java.awt.BorderLayout()); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(aaa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(574, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jLabelTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1).addContainerGap(249, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGap(15, 15, 15) .addComponent(aaa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO dadd your handling code here: DefaultCategoryDataset barChart = new DefaultCategoryDataset(); barChart.setValue(200, "Contribution Amount", "January"); barChart.setValue(550, "Contribution Amount", "February"); barChart.setValue(300, "Contribution Amount", "March"); JFreeChart jbarChart = ChartFactory.createBarChart("Bar chart", "Monthly", "Contribution Amount", barChart, PlotOrientation.HORIZONTAL, true, true, true); CategoryPlot barchrt = (CategoryPlot) jbarChart.getPlot(); barchrt.setRangeGridlinePaint(Color.BLUE); ChartPanel barPanel = new ChartPanel(jbarChart); barPanel.setPreferredSize(new Dimension(785, 440)); //size according to my window barPanel.setMouseWheelEnabled(true); JPanel jPanel = new JPanel(); jPanel.add(barPanel); String filename = "F:barchart.jpg"; try { ChartUtilities.saveChartAsJPEG(new File(filename), jbarChart, 500, 300); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton1ActionPerformed private void aaaAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_aaaAncestorAdded // TODO add your handling code here: DefaultCategoryDataset barChart = new DefaultCategoryDataset(); barChart.setValue(200, "Contribution Amount", "January"); barChart.setValue(550, "Contribution Amount", "February"); barChart.setValue(300, "Contribution Amount", "March"); JFreeChart jbarChart = ChartFactory.createBarChart("Bar chart", "Monthly", "Contribution Amount", barChart, PlotOrientation.HORIZONTAL, true, true, true); CategoryPlot barchrt = (CategoryPlot) jbarChart.getPlot(); barchrt.setRangeGridlinePaint(Color.BLUE); ChartPanel barPanel = new ChartPanel(jbarChart); aaa.removeAll(); aaa.setLayout(new BorderLayout()); aaa.add(barPanel, BorderLayout.CENTER); aaa.validate(); }//GEN-LAST:event_aaaAncestorAdded /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel aaa; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabelTitle; // End of variables declaration//GEN-END:variables }