1

我想绘制JPEG的系数直方图。我在 Google 上搜索了几个小时以了解如何使用Chart2D库,但没有示例教程。我要绘制的数组是hist[]. 我创建了一个对象LBChart2D,但我不知道如何将数组设置为数据集。

//coeff[] is the coefficients array
for(int i=0;i<coeff.length;i++)
hist[coeff[i]]++;

LBChart2D lbChart2D = new LBChart2D();

编辑:这是我正在尝试的:

Object2DProperties object2DProps = new Object2DProperties();
object2DProps.setObjectTitleText ("Title ");
Chart2DProperties chart2DProps = new Chart2DProperties();
chart2DProps.setChartBetweenChartAndLegendGapThicknessModel(5);
LegendProperties legendProps = new LegendProperties();
legendProps .setLegendBorderThicknessModel(5);
legendProps.setLegendBackgroundColor(Color.yellow);
legendProps.setLegendExistence (false);
GraphChart2DProperties graph2DProps = new GraphChart2DProperties();
GraphProperties graphProps = new GraphProperties();
object2DProps .setObjectTitleFontName("test");
Dataset dataset = new Dataset (1, hist.length, 1);
for(int i=0;i<hist.length;i++)
dataset.set (0, i, 0, hist[i]) ; 
LBChart2D lbChart2D = new LBChart2D();
lbChart2D.setObject2DProperties (object2DProps);
lbChart2D.setChart2DProperties (chart2DProps);
lbChart2D.setLegendProperties (legendProps);
lbChart2D.setGraphChart2DProperties (graph2DProps);
lbChart2D.addGraphProperties (graphProps);
lbChart2D.addDataset (dataset);
lbChart2D.setSize(width, height);
BufferedImage lbImage = lbChart2D.getImage();
jLabel15.setIcon(new ImageIcon(lbImage)); 

java.lang.NullPointerException现在它在这一行产生一个异常:

BufferedImage lbImage = lbChart2D.getImage();

怎么了?

4

2 回答 2

2

分发中包含几个演示。您可以从通过获得的数据收集数据。另请参阅Chart2D BufferedImageImageIO

附录:没有完整的例子,可以validate()用来获取调试信息。至少,验证您是否setLabelsAxisLabelsTexts()使用hist.length标签调用。

//Optional validation:  Prints debug messages if invalid only.
if (!chart2D.validate(false)) {
    chart2D.validate(true);
}
于 2012-04-01T14:05:52.633 回答
1

感谢@trashgod 试图帮助我。别担心,我已经拿到了。我使用 库来绘制直方图,这是我使用的代码。

int hist[]=new int[11];
int val[]=new int[11];
for(int ii=0;ii<11;ii++)
    hist[ii]=ii-5;//to get negative indeces I used an array to save them 
for(int kk=0;kk<coeff.length;kk++)
if(coeff[kk]<=5 &coeff[kk]>=-5) val[coeff[kk]+5]++;
DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
for(int ii=0;ii<hist.length;ii++)
dataset.setValue(val[ii], "Coefficient value",""+hist[ii]);
JFreeChart chart = ChartFactory.createBarChart("Original Histogram",
    "Coefficient value", "", dataset,
    PlotOrientation.VERTICAL, false,true, false);      
//chart.setBackgroundPaint(Color.yellow); 
chart.getTitle().setPaint(Color.blue); 
CategoryPlot p = chart.getCategoryPlot(); 
p.setOutlinePaint(Color.BLUE);
p.setRangeGridlinePaint(Color.blue); 
orgim=chart.createBufferedImage(400,400);
Image im1= orgim.getScaledInstance(jLabel12.getWidth(),jLabel12.getHeight(),1);
jLabel12.setIcon(new ImageIcon(im1));
///
于 2012-04-04T13:30:32.903 回答