Change JFreeChart Histogram colors dynamically?
<p>I'm trying to show the histogram of an image and show only some colors. I've already done that with JFreeChart and createXYLineChart and getting all the data by iterating over all pixels. </p>

<p>To speed it up I'm trying to do it with "createHistogram". I've followed <a href="https://stackoverflow.com/questions/28519355/displaying-a-histogram-of-image-data/28519356#28519356">this code</a>.</p>

<p>To update the chart with the new values I've used this two methods:</p>

<pre><code>XYPlot plot = (XYPlot) chart.getPlot();
plot.setDataset(setHistogram(red, green, blue));
</code></pre>

<p>Being setHistogram a method which returns a HistogramDataset depending on which checkbox are activated (boolean red, green and blue).</p>

<p>That works properly as intended.</p>

<p>The second thing I must do when I do that is update the colors of each series, as otherwise they get the default value. I did it following more or less the same procedure as with the values:</p>

<pre><code>private void setHistogramColors(boolean red, boolean green, boolean blue) {
    XYPlot plot = (XYPlot) chart.getPlot();
    XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
    renderer.setBarPainter(new StandardXYBarPainter());
    Paint[] paintArray = null;
    if (red) {
        if (green) {
            if (blue) {
                paintArray = new Paint[3];
                paintArray[0] = new Color(0x80ff0000, true);// translucent red, green &amp; blue
                paintArray[1] = new Color(0x8000ff00, true);
                paintArray[2] = new Color(0x800000ff, true);
            } else {
                paintArray = new Paint[2];
                paintArray[0] = new Color(0x80ff0000, true);
                paintArray[1] = new Color(0x8000ff00, true);
            }
        } else {
            paintArray = new Paint[1];
            paintArray[0] = new Color(0x80ff0000, true);
        }
    } else if (green) {
        if (blue) {
            paintArray = new Paint[2];
            paintArray[0] = new Color(0x8000ff00, true);
            paintArray[1] = new Color(0x800000ff, true);
        } else {
            paintArray = new Paint[1];
            paintArray[0] = new Color(0x8000ff00, true);
        }
    } else if (blue){
        paintArray = new Paint[1];
        paintArray[0] = new Color(0x800000ff, true);
    }
    else {
        return;
    }
    plot.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));
}
</code></pre>

<p>But for what I can see this only works when I plot the code for the very first time, and the next plots of different series will take the sames colors. Here is an example, RGB are the color they are supposed to be:
[<img src="https://i.stack.imgur.com/AqugL.png" alt="2]">
When I update with the red color goes to the green histogram and green's to the blue one:
<a href="https://i.stack.imgur.com/2jQny.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2jQny.png" alt=""></a></p>

<p>Is there anyway to fix this?</p>