如何更改XYbarchart的默认颜色(红色)[jfreechart]

时间:2014-04-19 23:33:43

标签: java jfreechart

以显示图像像素的灰度直方图 我使用了一个对象" XYBARCHART" [JFreeChart]我还使用了类别XYSeriesCollection和" XYItemRenderer"的数据集。渲染器。 我用方法改变颜色:这个。 renderer1.setSeriesPaint(i2,Color.green) 但该系列仍保持粉红色(默认)。我想要的是当我点击红色Boutton时我用红色,蓝色Boutton蓝色直方图显示直方图显示..

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

public class ImgHst implements ActionListener{

 private ParameterBlock imgParamBloc;
 private PlanarImage imgHistSrc  ;
 private Histogram imgHistogram;
 private int[] histValeurs;
 private int bandesCouleurs; //R;G;B
 private int maxHistValeurs;

    private ChartPanel myChartcontentPane;
    private JButton   redBtn,greenBtn,blueBtn;
    private XYSeriesCollection  ensHistg ;

    private JFrame frame;
    private JFreeChart myChart ;
    private XYSeries s,sred,sgreen,sblue;
    private XYPlot plot ;

    private  XYItemRenderer renderer0,renderer1,renderer2;


 ImgHst(PlanarImage img)
   {

     this.imgHistSrc =img ;
     imgHistogram  = new Histogram(256, 0, 255, 3); 
     this.histValeurs =imgHistogram .getBins(bandesCouleurs);
      maxHistValeurs= 0;
     for(int i=0;i<histValeurs.length;i++) maxHistValeurs = Math.max(maxHistValeurs,histValeurs[i]);
    imgParamBloc = new ParameterBlock();
    imgParamBloc.addSource(img);
    imgParamBloc.add(null);
    imgParamBloc.add(1);
    imgParamBloc.add(1);
    // création de l'histograme et remplissage du tableau des niveaux de gris 
    RenderedOp operateurHtg =  JAI.create("histogram", imgParamBloc, null);
    imgHistogram = (Histogram) operateurHtg.getProperty("histogram");
    createHstFrame();
  }
public void setImgSrc(PlanarImage img)
{
    imgHistSrc = img;
}
public Histogram   getMyHistogram()
{   
    return imgHistogram;
}
private XYSeries createSerie(int bande_couleur)
{
    XYSeries s = new XYSeries("S");
       for(int i=0;i < imgHistogram.getNumBins(bande_couleur);i++) 
       {
        s.add(i, imgHistogram.getBinSize(bande_couleur,i));
       }    
       return s;
}

public void setBandeCouleur(int b)
{
    this.bandesCouleurs =b;
}
public void createHstFrame()
 { 
      sred=createSerie(0);
      sgreen=createSerie(1);
      sblue=createSerie(2);


         myChart = ChartFactory.createXYBarChart("Histograme", "Intensité",false, "Nombre des Pixels",  ensHistg ,PlotOrientation.VERTICAL ,false,true,true);
         myChartcontentPane =new ChartPanel(myChart);
        // this.myChartcontentPane.setChart(ChartFactory.createXYBarChart("Histograme", "Intensité",false,"Nombre des Pixels", ensHistg,PlotOrientation.VERTICAL ,false,true,true));
          ChartUtilities.applyCurrentTheme(myChart);

         frame = new JFrame("");


     frame.add(myChartcontentPane);

    JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    redBtn = new JButton("Red");
    redBtn.addActionListener((ActionListener)this);
    panel.add(redBtn);
    greenBtn = new JButton("Green");
    greenBtn.addActionListener((ActionListener)this);
    panel.add(greenBtn);
    blueBtn = new JButton("Blue");
    blueBtn.addActionListener((ActionListener)this);
    panel.add(blueBtn);
    frame.add(panel, BorderLayout.SOUTH);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
//  return frame;
 }
public void createMyChart(XYSeries ss)
{      

         ensHistg  = new XYSeriesCollection(ss);
        myChartcontentPane.setChart(ChartFactory.createXYBarChart("Histograme", "Intensité",false, "Nombre des Pixels",  ensHistg,PlotOrientation.VERTICAL ,false,true,true));
        ChartUtilities.applyCurrentTheme(myChart);

         plot=    (XYPlot) myChart.getPlot();  
       ValueAxis xAxis =   plot.getDomainAxis();
      // ((ValueAxis) xAxis).setLowerBound(0);
     NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
      yAxis.setLowerBound(0);


        switch(this. bandesCouleurs)
        {
        case 0: 
                this. renderer0 =   plot.getRenderer() ;
                this. renderer0 =   plot.getRenderer() ;
                int i1 = ensHistg.indexOf(ss);

                this. renderer0.setSeriesPaint(i1, Color.green )    ;System.out.println(bandesCouleurs); 
                plot.setRenderer(   renderer0);
                break;

        case 1:  
                this. renderer1 =   plot.getRenderer() ;
                this. renderer1 =   plot.getRenderer() ;
               int i2 = ensHistg.indexOf(ss);
               this. renderer1.setSeriesPaint(i2, Color.green ) ;System.out.println(bandesCouleurs); 
               plot.setRenderer( renderer1);
                break;

        case 2 :this. renderer2 =  plot.getRenderer() ;
                this. renderer2 =  plot.getRenderer() ;
                this. renderer2.setSeriesPaint(0, Color.blue )  ;System.out.println(bandesCouleurs);
                plot.setRenderer(renderer2);
                break;
       }


        frame.add(myChartcontentPane);
        frame.repaint();

}
public void actionPerformed(ActionEvent cliquebtn) {


    if (cliquebtn.getSource().equals(redBtn))
    {
    setBandeCouleur(0);
    createMyChart(sred);
    }
    if (cliquebtn.getSource().equals(greenBtn))
    {
    setBandeCouleur(1);
    createMyChart(sgreen);

    }
    if (cliquebtn.getSource().equals(blueBtn))
    {   setBandeCouleur(2); 
        createMyChart(sblue);
    }
    }

}

2 个答案:

答案 0 :(得分:1)

您的createMyChart()方法有一行为ChartPanel分配新图表,但它与您在myChart字段中引用的图表对象不同:

    myChartcontentPane.setChart(ChartFactory.createXYBarChart("Histograme", "Intensité",false, "Nombre des Pixels",  ensHistg,PlotOrientation.VERTICAL ,false,true,true));

答案 1 :(得分:0)

有效 先生,我发现了你说的话。 我通过以下方式解决了我的问题,但我对编码不满意

    import java.awt.BorderLayout;
    import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.Histogram;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ImgHst implements ActionListener{

     private ParameterBlock imgParamBloc;
     private Histogram imgHistogram;
     private int[] histValeurs;
     private int bandesCouleurs; //R;G;B
     private int maxHistValeurs;
     private JButton   redBtn,greenBtn,blueBtn;
     private XYSeriesCollection  ensHistg ;
     private JFrame frame;
     private XYPlot plot ;


     ImgHst(PlanarImage img)
       {

        imgHistogram  = new Histogram(255, 0, 255, 3); 
        this.histValeurs =imgHistogram .getBins(bandesCouleurs);
        maxHistValeurs= 0;
        for(int i=0;i<histValeurs.length;i++) maxHistValeurs = Math.max(maxHistValeurs,histValeurs[i]);
        imgParamBloc = new ParameterBlock();
        imgParamBloc.addSource(img);
        imgParamBloc.add(null);
        imgParamBloc.add(1);
        imgParamBloc.add(1);
        // création de l'histograme et remplissage du tableau des niveaux de gris 
        RenderedOp operateurHtg =  JAI.create("histogram", imgParamBloc, null);
        imgHistogram = (Histogram) operateurHtg.getProperty("histogram");
        createHstFrame();
      }
    public void setImgSrc(PlanarImage img)
    {
    }
    public Histogram   getMyHistogram()
    {   
        return imgHistogram;
    }
    private XYSeries createSerie(int bande_couleur)
    {
        XYSeries s = new XYSeries("S");
           for(int i=0;i < imgHistogram.getNumBins(bande_couleur);i++) 
           {
            s.add(i, imgHistogram.getBinSize(bande_couleur,i));
           }    
           return s;
    }

    public void setBandeCouleur(int b)
    {
        this.bandesCouleurs =b;
    }
    public ChartPanel getChartPane(int band)
    {  
        ensHistg = new XYSeriesCollection(createSerie(band));
        JFreeChart  Chart = ChartFactory.createXYBarChart("Histograme", "Intensité",false, "Nombre des Pixels",  ensHistg ,PlotOrientation.VERTICAL ,false,true,true);
        ChartPanel cPanel= new  ChartPanel(Chart);
        plot=    (XYPlot)  Chart.getPlot();  
        ValueAxis xAxis =   plot.getDomainAxis();
        ((ValueAxis) xAxis).setLowerBound(0);
        NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
        yAxis.setLowerBound(0);
        XYBarRenderer barRndr = (XYBarRenderer) plot.getRenderer();
        switch(band)
        {
        case 0: barRndr.setSeriesPaint(0, Color.red )   ;  
                plot.setRenderer( barRndr);
                break;

        case 1:  barRndr.setSeriesPaint(0, Color.green ); 
                 plot.setRenderer( barRndr);
                 break;


        case 2 :  barRndr.setSeriesPaint(0, Color.blue ); 
                  plot.setRenderer( barRndr);
                  break;
       }

        return cPanel;
    }
    public void createHstFrame()
     { 
            frame = new JFrame("");
            frame.add(setBtnPanel(), BorderLayout.SOUTH);
            frame.pack();
            frame.setVisible(true);

     }
    private JPanel setBtnPanel()
    {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        redBtn = new JButton("Red");
        redBtn.addActionListener((ActionListener)this);
        panel.add(redBtn);
        greenBtn = new JButton("Green");
        greenBtn.addActionListener((ActionListener)this);
        panel.add(greenBtn);
        blueBtn = new JButton("Blue");
        blueBtn.addActionListener((ActionListener)this);
        panel.add(blueBtn);
        return panel;
    }


    public void actionPerformed(ActionEvent cliquebtn) {


        if (cliquebtn.getSource().equals(redBtn))
        {
            frame.getContentPane().removeAll();
            frame.getContentPane().repaint();
            frame.add(getChartPane(0));
            frame.add(setBtnPanel(), BorderLayout.SOUTH);
            frame.pack();

        }
        if (cliquebtn.getSource().equals(greenBtn))
        {     
            frame.getContentPane().removeAll();
            frame.getContentPane().repaint();
            frame.add(getChartPane(1));
            frame.add(setBtnPanel(), BorderLayout.SOUTH);
            frame.pack();


        }
        if (cliquebtn.getSource().equals(blueBtn))
        {    
            frame.getContentPane().removeAll();
            frame.getContentPane().repaint();
            frame.add(getChartPane(2));
            frame.add(setBtnPanel(), BorderLayout.SOUTH);
            frame.pack();

        }
        }

}