package chap15;

import javax.swing.*;
import java.awt.*;

public class Histogram extends JPanel {

    private int[] count;


    public void showHistogram(int[] count) {
        this.count = count;
    }

    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    protected void paintComponent(Graphics graphics) {
        if (count == null) return;

        super.paintComponent(graphics);

        int width = getWidth();
        int height = getHeight();
        int interval = (width - 40) / count.length;
        int individualWidth = (int) (((width - 40) / 24) * 0.60);

        int maxCount = 0;
        for (int i = 0; i < count.length; i++) {
            if (maxCount < count[i]) {
                maxCount = count[i];
            }
        }

        int x = 30;
        graphics.drawLine(10, height - 45, width - 10, height - 45);
        for (int i = 0; i < count.length; i++) {
            int barHeight = (int) (((double) count[i] / (double) maxCount) * (height - 55));
            graphics.drawRect(x, height - 45 - barHeight, individualWidth, barHeight);
            graphics.drawString((char) (65 + i) + " ", x, height - 30);
            x += interval;
        }
    }

}
