0

我的 jfreeChart 来自网络摄像头的框架。我传递了这些帧,它会生成该帧的 R、G 和 B 颜色图形。我希望这个图表在我传递帧时自动更新。我已经尝试了一切,但没有任何效果。

欢迎任何帮助。

class DaemonThread implements Runnable
{
    protected volatile boolean runnable = false;

    @Override
    public  void run()
    {
        synchronized(this)
        {
            while(runnable)
            {
                if(webSource.grab())
                {
                    try
                    {
                        webSource.retrieve(frame);
                        Imgcodecs.imencode(".bmp", frame, mem);
                        Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));

                        buff = (BufferedImage) im;

                        if(contHistograma==0) {
                            h = new Histo(buff);
                            contHistograma++;
                        }else{
                            h.update(buff);
                        }   

                        webcamLabel.setIcon(new ImageIcon(buff));

                        Graphics g=webcamPanel.getGraphics();

                        if (g.drawImage(buff, 0, 0, getWidth(), getHeight() -150 , 0, 0, buff.getWidth(), buff.getHeight(), null))

                            if(runnable == false)
                            {
                                System.out.println("Going to wait()");
                                this.wait();
                            }
                    }
                    catch(Exception ex)
                    {
                        System.out.println("Error");

                    }
                }
            }
        }
    }
}





public class Histo{
private ChartFrame lineFrame;
private ArrayList<Integer> redArray;
private ArrayList<Integer> greenArray;
private ArrayList<Integer> blueArray;


public Histo(BufferedImage originalImage){
    Color c;

    redArray = new ArrayList<>();
    greenArray = new ArrayList<>();
    blueArray = new ArrayList<>();

    for (int i = 0; i < originalImage.getWidth(null); i++) {
        for (int j = 0; j < originalImage.getHeight(null); j++) {
            c = new Color(originalImage.getRGB(i,j));
            redArray.add(c.getRed());
            greenArray.add(c.getGreen());
            blueArray.add(c.getBlue());
        }
    }
    Collections.sort(redArray);
    Collections.sort(greenArray);
    Collections.sort(blueArray);


    XYSeriesCollection dataset = creatingDataset(redArray, greenArray, blueArray);


    JFreeChart chart = createChart(dataset);

    ChartPanel chartPanel = new ChartPanel(chart);

    lineFrame = new ChartFrame("teste", chart);
    lineFrame.add(chartPanel);
    lineFrame.setVisible(true);
    lineFrame.setSize(600, 450);
}

public void update(BufferedImage originalImage){
    redArray.clear();
    greenArray.clear();
    blueArray.clear();

    Color c;

    for (int i = 0; i < originalImage.getWidth(null); i++) {
        for (int j = 0; j < originalImage.getHeight(null); j++) {
            c = new Color(originalImage.getRGB(i,j));
            redArray.add(c.getRed());
            greenArray.add(c.getGreen());
            blueArray.add(c.getBlue());
        }
    }
    Collections.sort(redArray);
    Collections.sort(greenArray);
    Collections.sort(blueArray);

    XYSeriesCollection dataset = creatingDataset(redArray, greenArray, blueArray);
    JFreeChart chart = createChart(dataset);
    ChartPanel chartPanel = new ChartPanel(chart);

    lineFrame.removeAll();
    lineFrame.add(chartPanel);
    lineFrame.revalidate();

    lineFrame.setLayout(new BorderLayout());
    lineFrame.repaint();
}

public static XYSeriesCollection creatingDataset(ArrayList<Integer> redArray,ArrayList<Integer> greenArray,ArrayList<Integer> blueArray){
    int cont=1, cont2=1, cont3=1;
    XYSeries series = new XYSeries("Red");
    XYSeries series2 = new XYSeries("Green");
    XYSeries series3 = new XYSeries("Blue");

    for (int i = 0; i < redArray.size()-1; i++) {
        if(redArray.get(i).intValue() == redArray.get(i+1).intValue()) {
            cont++;
        }else{
            series.add(redArray.get(i).intValue(), cont);
            cont=1;
        }
    }

    for (int i = 0; i < greenArray.size()-1; i++) {
        if(greenArray.get(i).intValue() == greenArray.get(i + 1).intValue()) {
            cont2++;
        }else{
            series2.add(greenArray.get(i).intValue(), cont2);
            cont2 = 1;
        }
    }

    for (int i = 0; i < blueArray.size()-1; i++) {
        if(blueArray.get(i).intValue() == blueArray.get(i+1).intValue()) {
            cont3++;
        }else{
            series3.add(blueArray.get(i).intValue(), cont3);
            cont3=1;
        }
    }

    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series);
    dataset.addSeries(series2);
    dataset.addSeries(series3);

    return dataset;
}

private JFreeChart createChart(final XYDataset dataset) {
    JFreeChart result = ChartFactory.createXYLineChart("Gráfico XY", "Eixo X", "Eixo Y", dataset, PlotOrientation.VERTICAL, true, true, false);


    final XYPlot plot = result.getXYPlot();

    plot.setBackgroundPaint(new Color(0xffffe0));
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.lightGray);

    final NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();               //RANGE EIXO X
    domainAxis.setRange(0, 260);
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();                 //RANGE EIXO Y
    rangeAxis.setRange(0, 5000);

    return result;
}

}

4

1 回答 1

0

如果其他人有这个问题,这里是答案:

Chartframe 摆脱不了 chartpanel,所以我把 Chartpanel 做了一个属性。之后,我更新了我的数据集并执行了此操作:

JFreeChart chart = createChart(dataset);
chartPanel.setChart(chart);
lineFrame.setLayout(new BorderLayout());
lineFrame.repaint();

问题解决了。

于 2016-05-18T00:09:48.620 回答