import java.awt.Color; import java.awt.Graphics; import javax.swing.JComponent; public class Histogram extends JComponent { private Values val; private Color[] colors = { Color.RED, Color.BLUE, Color.GREEN, Color.CYAN, Color.MAGENTA, Color.PINK }; public Histogram(Values val) { this.val = val; } public void paintComponent(Graphics g) { int w = getWidth(); int h = getHeight(); if (isOpaque()) { g.setColor(getBackground()); g.fillRect(0, 0, w, h); } int width = w / val.size(); double unitHeight = h / val.sum(); for (int i = 0; i < val.size(); i++) { int hr = (int) (unitHeight * val.value(i)); g.setColor(colors[i % colors.length]); g.fillRect(i * width, h - hr, width, hr); } } }