1

I want to draw coefficient histogram of JPEG. I'm searching on Google for hours to know how to use Chart2D library, but there is no tutorial with examples. The array which I want to draw is hist[]. I created an object of LBChart2D, but I don't know how to set the array as data set to it.

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

LBChart2D lbChart2D = new LBChart2D();

EDIT:Here is what I'm trying :

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)); 

Now It produces an Exception java.lang.NullPointerException on this line:

BufferedImage lbImage = lbChart2D.getImage();

What's wrong?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
muhannad
  • 41
  • 2
  • 10

2 Answers2

2

Several Chart2D demos are included in the distribution. You can collect the data from a BufferedImage obtained via ImageIO. See also .

Addendum: Absent a complete example, you can use validate() to get debug messages. At a minimum, verify that you invoke setLabelsAxisLabelsTexts() with hist.length labels.

//Optional validation:  Prints debug messages if invalid only.
if (!chart2D.validate(false)) {
    chart2D.validate(true);
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • is there any tutorial with examples that could help me? – muhannad Apr 01 '12 at 14:09
  • I'd start with [The Chart2D Tutorial](http://chart2d.sourceforge.net/Chart2D_1.9.6/Tutorial/Tutorial.htm). – trashgod Apr 01 '12 at 14:12
  • They're in the source distribution. – trashgod Apr 01 '12 at 14:19
  • 1
    What's wrong with `Chart2D/LBChart2DFrameDemo.java`? It shows over a dozen bar chart variations. See also the [faq]; this isn't a free coding service. – trashgod Apr 01 '12 at 14:37
  • well,I asked HOW TO set the array as data set ? ,anybody knows tell me, thanks in advance – muhannad Apr 01 '12 at 14:43
  • Use the `getRGB()` method found in `BuffereImage` to collect the data, as suggested above. – trashgod Apr 01 '12 at 14:48
  • what data??? I want to set the array that contains information about the coefficient to draw as two coordinates ,one for the coefficient value and the other is for the coefficient count ,that's it – muhannad Apr 01 '12 at 14:54
1

Thanks @trashgod for trying to help me. Don't worry I've got it. I used library to draw the histogram, and here is the code I used.

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));
///
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
muhannad
  • 41
  • 2
  • 10