我试图显示图像的直方图并仅显示一些颜色。我已经使用JFreeChart和createXYLineChart完成了这项工作,并通过遍历所有像素获取所有数据。
为了加快速度,我尝试用" createHistogram"来做到这一点。我跟着this code。
要使用新值更新图表,我使用了以下两种方法:
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDataset(setHistogram(red, green, blue));
setHistogram是一个方法,它根据激活的复选框返回HistogramDataset(布尔红色,绿色和蓝色)。
按预期正常工作。
当我这样做时,我必须做的第二件事是更新每个系列的颜色,否则它们会得到默认值。我按照与值相同的程序进行了操作:
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 & 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));
}
但是对于我所看到的情况,这只有在我第一次绘制代码时才有效,而下一个不同系列的绘图将采用相同的颜色。这是一个例子,RGB是它们应该是的颜色: [ 当我用红色更新到绿色直方图并且绿色到蓝色时:
有没有解决这个问题?
答案 0 :(得分:3)
根据您的片段和此comment,您似乎试图通过在复选框处理程序中添加和删除数据集中的系列来更改显示。而不是更改模型,而是在视图中更改系列的可见性。在这种情况下,您可以为每个复选框指定合适的Action
,如下所示here。请特别注意VisibleAction
使用与自定义DrawingSupplier
中相应颜色匹配的系列索引。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
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;
/**
* @see https://stackoverflow.com/q/40537278/230513
* @see https://stackoverflow.com/q/11870416/230513
* @see https://stackoverflow.com/a/28519356/230513
*/
public class Histogram {
private static final int BINS = 256;
private final BufferedImage image = getImage();
private HistogramDataset dataset;
private XYBarRenderer renderer;
private BufferedImage getImage() {
try {
return ImageIO.read(new URL(
"http://i.imgur.com/kxXhIH1.jpg"));
} catch (IOException e) {
e.printStackTrace(System.err);
}
return null;
}
private ChartPanel createChartPanel() {
// dataset
dataset = new HistogramDataset();
Raster raster = image.getRaster();
final int w = image.getWidth();
final int h = image.getHeight();
double[] r = new double[w * h];
r = raster.getSamples(0, 0, w, h, 0, r);
dataset.addSeries("Red", r, BINS);
r = raster.getSamples(0, 0, w, h, 1, r);
dataset.addSeries("Green", r, BINS);
r = raster.getSamples(0, 0, w, h, 2, r);
dataset.addSeries("Blue", r, BINS);
// chart
JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value",
"Count", dataset, PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
renderer = (XYBarRenderer) plot.getRenderer();
renderer.setBarPainter(new StandardXYBarPainter());
// translucent red, green & blue
Paint[] paintArray = {
new Color(0x80ff0000, true),
new Color(0x8000ff00, true),
new Color(0x800000ff, true)
};
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));
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}
private JPanel createControlPanel() {
JPanel panel = new JPanel();
panel.add(new JCheckBox(new VisibleAction(0)));
panel.add(new JCheckBox(new VisibleAction(1)));
panel.add(new JCheckBox(new VisibleAction(2)));
return panel;
}
private class VisibleAction extends AbstractAction {
private final int i;
public VisibleAction(int i) {
this.i = i;
this.putValue(NAME, (String) dataset.getSeriesKey(i));
this.putValue(SELECTED_KEY, true);
renderer.setSeriesVisible(i, true);
}
@Override
public void actionPerformed(ActionEvent e) {
renderer.setSeriesVisible(i, !renderer.getSeriesVisible(i));
}
}
private void display() {
JFrame f = new JFrame("Histogram");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createChartPanel());
f.add(createControlPanel(), BorderLayout.SOUTH);
f.add(new JLabel(new ImageIcon(image)), BorderLayout.WEST);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Histogram().display();
});
}
}