Jfreechart直方图设置半透明颜色

时间:2015-08-23 07:49:01

标签: java image-processing jfreechart

我有这样的直方图 histogram

这里红色直方图在前面,蓝色在中间,绿色在后面。所以绿色直方图的大部分都看不到。有没有选项可以在悬停鼠标或任何其他选项之间切换颜色以查看绿色和蓝色直方图。

这是代码

public class HistogramFrame extends JFrame {

    static double[] red;
    static double[] green;
    static double[] blue;

    public HistogramFrame(String title, double[] red, double[] green, double[] blue) {
        super(title);
        HistogramFrame.red = red;
        HistogramFrame.green = green;
        HistogramFrame.blue = blue;

        JFreeChart jfreechart = ChartFactory.createHistogram("Histograms combination red,blue,green", null, null, createDataset(), PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        xyplot.setForegroundAlpha(0.85F);
        XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
        xybarrenderer.setDrawBarOutline(false);
        JPanel jpanel = new ChartPanel(jfreechart);
        jpanel.setPreferredSize(new Dimension(1000, 600));
        setContentPane(jpanel);
    }

    private static IntervalXYDataset createDataset() {
        HistogramDataset histogramdataset = new HistogramDataset();
        histogramdataset.addSeries("Red histogram", red, 256);
        histogramdataset.addSeries("Blue histogram", blue, 256);
        histogramdataset.addSeries("Green histogram", green, 256);

        return histogramdataset;
    }

}

1 个答案:

答案 0 :(得分:1)

以下是解决方案和图形图像的完整代码。现在半透明的颜色可以更清晰地看到每个直方图。

Translucent Histogram

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.xy.IntervalXYDataset;

public class HistogramFrame extends JFrame {

    static double[] red;
    static double[] green;
    static double[] blue;

    public HistogramFrame(String title, double[]     red, double[] green, double[] blue) {
        super(title);
        HistogramFrame.red = red;
        HistogramFrame.green = green;
        HistogramFrame.blue = blue;

        JFreeChart jfreechart = ChartFactory.createHistogram("Histograms combination red,blue,green", null, null, createDataset(), PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        xyplot.setForegroundAlpha(0.85F);
        XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
        xybarrenderer.setBarPainter(new StandardXYBarPainter());
        //xybarrenderer.setDrawBarOutline(false);
        Paint[] paintArray = {              //code related to translucent colors begin here
            new Color(0x80ff0000, true),
            new Color(0x8000ff00, true),
            new Color(0x800000ff, true)
        };

        xyplot.setDrawingSupplier(new DefaultDrawingSupplier(
            paintArray,
            DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));

        JPanel jpanel = new ChartPanel(jfreechart);
        jpanel.setPreferredSize(new Dimension(1000, 600));
        setContentPane(jpanel);
    }

    private static IntervalXYDataset createDataset() {
        HistogramDataset histogramdataset = new HistogramDataset();
        histogramdataset.addSeries("Red histogram", red, 256);
        histogramdataset.addSeries("Blue histogram", blue, 256);
        histogramdataset.addSeries("Green histogram", green, 256);

        return histogramdataset;
    }

}