GUI组件,图形和图表。

时间:2012-01-24 05:17:18

标签: java swing user-interface graph

好的,所以我很难过。有没有人知道使用NetBeans的Java的简单方法,它将使用jPanel上的绘图组件绘制图形(最好是饼图)?

我用谷歌搜索,研究过,找不到直接的答案。我还在学习,这是本课程最后一部分。我只需要这个方法,如果有人知道一个简单的方法来做到这一点。它不需要改变,因为要求表明我只需要一个图形;程序逻辑从temp.textField解析。非常感谢任何帮助或指导。

P.S。是的,我尝试过JFreeCharts。

private void jPanel1ComponentShown(java.awt.event.ComponentEvent evt) {

    // Bar graph component and logic. 

    BorderLayout panelMapLayout = new BorderLayout();

    jPanel1.setLayout(panelMapLayout);
    jPanel1.add(Graph, BorderLayout.CENTER);

    JFrame fr = new JFrame();
   final int width = 300;
   final int height = 400;
   fr.setSize(width, height);
   fr.setTitle("Grade Bar Graph");
   fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   fr.setVisible(true);
   String str = JOptionPane.showInputDialog("What is the String file name?");
    barGraphComponent component = new barGraphComponent();

    fr.setVisible(true);


}

以上是我的代码 - 只是一部分,但我认为这将有效。任何人都有任何想法如何添加逻辑?如果需要,我将抛出整个代码。我不需要它来改变,只显示一组初始整数;但是如何将这些整数插入到我的图表中?

1 个答案:

答案 0 :(得分:0)

使用JCommon& JFreechart罐子。
我希望你能和它一起去......

import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

  public class JavaApplication3 {
        DefaultCategoryDataset categoryDataset;
        private String YaxisXaxis = "Hospital mortality rate";
        private String Xaxis[] = {"2009APR-2010MAR", "2010APR-2011MAR", "2011APR-2011NOV"};
        double val[] = {1.7879989, 1.6252073, 1.5941324};
     JFreeChart chart ;
        public void getinput() {              
        }
        void setinp() {
            categoryDataset = new DefaultCategoryDataset();
            for (int i = 0; i < val.length; i++) {
                categoryDataset.setValue(val[i], YaxisXaxis, Xaxis[i]);
            }
        }
        void drawcharts(){
             chart = ChartFactory.createBarChart3D("XYZ HOSPITALS", // Title
                          "Year",              // X-Axis label
                          "Number of Students",// Y-Axis label
                          categoryDataset,         // Dataset
                          PlotOrientation.VERTICAL,
                          true,                     // Show legend
                          true,
                          false
                         );
        }
        void savechart()
        {
                  String fileName="d:/sp.jpg";
            try {
                /**
                 * This utility saves the JFreeChart as a JPEG
                 * First Parameter: FileName
                 * Second Parameter: Chart To Save
                 * Third Parameter: Height Of Picture
                 * Fourth Parameter: Width Of Picture
                 */
            ChartUtilities.saveChartAsJPEG(new File(fileName), chart, 800, 600);
        } catch (IOException e) {
            System.err.println("Problem occurred creating chart.");
        }
        }
        public static void main(String[] args) {
            // TODO code application logic here
           JavaApplication3 obj =new  JavaApplication3();
           obj.setinp();
           obj.drawcharts();
           obj.savechart();

        }
    }

试试这个样本......你可以轻松搞定......