Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.geom.Rectangle2D;
- import java.util.Arrays;
- import java.util.Map;
- import java.util.TreeMap;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- public class Histogram {
- public long[][] histogramArr;
- public int size;
- Map<Long, Long> mapHistory;
- public Histogram(long[] xBounds, long[] yBounds) {
- this.size = xBounds.length;
- histogramArr = new long[xBounds.length][2];
- for (int i = 0; i < xBounds.length; i++) {
- histogramArr[i][0] = xBounds[i];
- histogramArr[i][1] = yBounds[i];
- System.out.println(xBounds[i] + " " + yBounds[i]);
- }
- }
- public void drawHistogram() {
- mapHistory = new TreeMap<Long, Long>();
- for (int c = 0; c < size; c++) {
- for (int r = 0; r < histogramArr[c].length; r++) {
- long value = histogramArr[c][r];
- long amount = 0;
- if (mapHistory.containsKey(value)) {
- amount = mapHistory.get(value);
- amount++;
- } else {
- amount = 1;
- }
- mapHistory.put(value, amount);
- }
- }
- JFrame frame = new JFrame("Test");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.add(new JScrollPane(new Graph(mapHistory)));
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- protected class Graph extends JPanel {
- protected static final int MIN_BAR_WIDTH = 4;
- private Map<Long, Long> mapHistory;
- public Graph(Map<Long, Long> mapHistory) {
- this.mapHistory = mapHistory;
- int width = (mapHistory.size() * MIN_BAR_WIDTH) + 11;
- Dimension minSize = new Dimension(width, 128);
- Dimension prefSize = new Dimension(width, 256);
- setMinimumSize(minSize);
- setPreferredSize(prefSize);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if (mapHistory != null) {
- int xOffset = 5;
- int yOffset = 5;
- int width = getWidth() - 1 - (xOffset * 2);
- int height = getHeight() - 1 - (yOffset * 2);
- Graphics2D g2d = (Graphics2D) g.create();
- g2d.setColor(Color.DARK_GRAY);
- g2d.drawRect(xOffset, yOffset, width, height);
- int barWidth = Math.max(MIN_BAR_WIDTH,
- (int) Math.floor((float) width
- / (float) mapHistory.size()));
- System.out.println("width = " + width + "; size = "
- + mapHistory.size() + "; barWidth = " + barWidth);
- long maxValue = 0;
- for (Long key : mapHistory.keySet()) {
- long value = mapHistory.get(key);
- maxValue = Math.max(maxValue, value);
- }
- int xPos = xOffset;
- for (Long key : mapHistory.keySet()) {
- long value = mapHistory.get(key);
- int barHeight = Math.round(((float) value
- / (float) maxValue) * height);
- g2d.setColor(Color.BLUE);
- int yPos = height + yOffset - barHeight;
- //Rectangle bar = new Rectangle(xPos, yPos, barWidth, barHeight);
- Rectangle2D bar = new Rectangle2D.Float(
- xPos, yPos, barWidth, barHeight);
- g2d.fill(bar);
- g2d.setColor(Color.DARK_GRAY);
- g2d.draw(bar);
- xPos += barWidth;
- }
- g2d.dispose();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement