Using the Chart2D library, the example below illustrates some alternative approaches.
A ColorConvertOp used to convert a sample image to shades of gray, as shown here and here .
Nested loops are repeated in pixels of the BufferedImage , calling the getRGB() method to retrieve the value of each pixel; appropriate calculations are used to build the dataset .

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GradientPaint; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.image.BufferedImage; import java.awt.image.ColorConvertOp; import javax.swing.BorderFactory; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import net.sourceforge.chart2d.Chart2DProperties; import net.sourceforge.chart2d.Dataset; import net.sourceforge.chart2d.GraphChart2DProperties; import net.sourceforge.chart2d.GraphProperties; import net.sourceforge.chart2d.LBChart2D; import net.sourceforge.chart2d.LegendProperties; import net.sourceforge.chart2d.MultiColorsProperties; import net.sourceforge.chart2d.Object2DProperties; public class Histogram extends JPanel { private BufferedImage image = getImage("OptionPane.warningIcon"); private BufferedImage gray = getGray(image); public Histogram() { JPanel panel = new JPanel(new GridLayout(0, 1)); panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); panel.add(new JLabel(new ImageIcon(image))); panel.add(new JLabel(new ImageIcon(gray))); this.setLayout(new BorderLayout()); this.add(panel, BorderLayout.WEST); this.add(createChart(gray, 20), BorderLayout.CENTER); } private BufferedImage getImage(String name) { Icon icon = UIManager.getIcon(name); int w = icon.getIconWidth(); int h = icon.getIconHeight(); this.setPreferredSize(new Dimension(w, h)); BufferedImage i = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = (Graphics2D) i.getGraphics(); g2d.setPaint(new GradientPaint( 0, 0, Color.blue, w, h, Color.green, true)); g2d.fillRect(0, 0, w, h); icon.paintIcon(null, g2d, 0, 0); g2d.dispose(); return i; } private BufferedImage getGray(BufferedImage image) { BufferedImage g = new BufferedImage( image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); ColorConvertOp op = new ColorConvertOp( image.getColorModel().getColorSpace(), g.getColorModel().getColorSpace(), null); op.filter(image, g); return g; } private LBChart2D createChart(BufferedImage gray, int buckets) {
source share