为了显示图像像素的灰度直方图,我使用了对象“ XYBARCHART” [JFreeChart],我还使用了类别XYSeriesCollection和“ XYItemRenderer”渲染器的数据集。 我用以下方法更改颜色:this。 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);
    }
    }

}

===============>>#1 票数:1

您的createMyChart()方法具有以下行,该行将新图表分配给ChartPanel ,但它与您在myChart字段中引用的图表对象myChart

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

===============>>#2 票数: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();

        }
        }

}

  ask by Sa mid translate from so

未解决问题?本站智能推荐: