Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.78 KB | None | 0 0
  1. import java.awt.BasicStroke;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Point;
  8. import java.awt.RenderingHints;
  9. import java.awt.Stroke;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Random;
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15. import javax.swing.SwingUtilities;
  16.  
  17. /*
  18.  * To change this template, choose Tools | Templates
  19.  * and open the template in the editor.
  20.  */
  21. /**
  22.  *
  23.  * @author Rodrigo
  24.  */
  25. public class GraphPanel extends JPanel {
  26.  
  27.     private int width = 800;
  28.     private int heigth = 400;
  29.     private int padding = 25;
  30.     private int labelPadding = 25;
  31.     private Color lineColor = new Color(44, 102, 230, 180);
  32.     private Color pointColor = new Color(100, 100, 100, 180);
  33.     private Color gridColor = new Color(200, 200, 200, 200);
  34.     private static final Stroke GRAPH_STROKE = new BasicStroke(2f);
  35.     private int pointWidth = 4;
  36.     private int numberYDivisions = 10;
  37.     private List<Long> scores;
  38.  
  39.     public GraphPanel(List<Long> scores) {
  40.         this.scores = scores;
  41.     }
  42.  
  43.     @Override
  44.     protected void paintComponent(Graphics g) {
  45.         super.paintComponent(g);
  46.         Graphics2D g2 = (Graphics2D) g;
  47.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  48.  
  49.         double xScale = ((double) getWidth() - (2 * padding) - labelPadding) / (scores.size() - 1);
  50.         double yScale = ((double) getHeight() - 2 * padding - labelPadding) / (getMaxScore() - getMinScore());
  51.  
  52.         List<Point> graphPoints = new ArrayList<>();
  53.         for (int i = 0; i < scores.size(); i++) {
  54.             int x1 = (int) (i * xScale + padding + labelPadding);
  55.             int y1 = (int) ((getMaxScore() - scores.get(i)) * yScale + padding);
  56.             graphPoints.add(new Point(x1, y1));
  57.         }
  58.  
  59.         // draw white background
  60.         g2.setColor(Color.WHITE);
  61.         g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding);
  62.         g2.setColor(Color.BLACK);
  63.  
  64.         // create hatch marks and grid lines for y axis.
  65.         for (int i = 0; i < numberYDivisions + 1; i++) {
  66.             int x0 = padding + labelPadding;
  67.             int x1 = pointWidth + padding + labelPadding;
  68.             int y0 = getHeight() - ((i * (getHeight() - padding * 2 - labelPadding)) / numberYDivisions + padding + labelPadding);
  69.             int y1 = y0;
  70.             if (scores.size() > 0) {
  71.                 g2.setColor(gridColor);
  72.                 g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1);
  73.                 g2.setColor(Color.BLACK);
  74.                 String yLabel = ((int) ((getMinScore() + (getMaxScore() - getMinScore()) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + "";
  75.                 FontMetrics metrics = g2.getFontMetrics();
  76.                 int labelWidth = metrics.stringWidth(yLabel);
  77.                 g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3);
  78.             }
  79.             g2.drawLine(x0, y0, x1, y1);
  80.         }
  81.  
  82.         // and for x axis
  83.         for (int i = 0; i < scores.size(); i++) {
  84.             if (scores.size() > 1) {
  85.                 int x0 = i * (getWidth() - padding * 2 - labelPadding) / (scores.size() - 1) + padding + labelPadding;
  86.                 int x1 = x0;
  87.                 int y0 = getHeight() - padding - labelPadding;
  88.                 int y1 = y0 - pointWidth;
  89.                 if ((i % ((int) ((scores.size() / 20.0)) + 1)) == 0) {
  90.                     g2.setColor(gridColor);
  91.                     g2.drawLine(x0, getHeight() - padding - labelPadding - 1 - pointWidth, x1, padding);
  92.                     g2.setColor(Color.BLACK);
  93.                     String xLabel = i + "";
  94.                     FontMetrics metrics = g2.getFontMetrics();
  95.                     int labelWidth = metrics.stringWidth(xLabel);
  96.                     g2.drawString(xLabel, x0 - labelWidth / 2, y0 + metrics.getHeight() + 3);
  97.                 }
  98.                 g2.drawLine(x0, y0, x1, y1);
  99.             }
  100.         }
  101.  
  102.         // create x and y axes
  103.         g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
  104.         g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, getWidth() - padding, getHeight() - padding - labelPadding);
  105.  
  106.         Stroke oldStroke = g2.getStroke();
  107.         g2.setColor(lineColor);
  108.         g2.setStroke(GRAPH_STROKE);
  109.         for (int i = 0; i < graphPoints.size() - 1; i++) {
  110.             int x1 = graphPoints.get(i).x;
  111.             int y1 = graphPoints.get(i).y;
  112.             int x2 = graphPoints.get(i + 1).x;
  113.             int y2 = graphPoints.get(i + 1).y;
  114.             g2.drawLine(x1, y1, x2, y2);
  115.         }
  116.  
  117.         g2.setStroke(oldStroke);
  118.         g2.setColor(pointColor);
  119.         for (int i = 0; i < graphPoints.size(); i++) {
  120.             int x = graphPoints.get(i).x - pointWidth / 2;
  121.             int y = graphPoints.get(i).y - pointWidth / 2;
  122.             int ovalW = pointWidth;
  123.             int ovalH = pointWidth;
  124.             g2.fillOval(x, y, ovalW, ovalH);
  125.         }
  126.     }
  127.  
  128. //    @Override
  129. //    public Dimension getPreferredSize() {
  130. //        return new Dimension(width, heigth);
  131. //    }
  132.  
  133.     private double getMinScore() {
  134.         double minScore = Double.MAX_VALUE;
  135.         for (Long score : scores) {
  136.             minScore = Math.min(minScore, score);
  137.         }
  138.         return minScore;
  139.     }
  140.  
  141.     private long getMaxScore() {
  142.         long maxScore = Long.MIN_VALUE;
  143.         for (Long score : scores) {
  144.             maxScore = Math.max(maxScore, score);
  145.         }
  146.         return maxScore;
  147.     }
  148.  
  149.     public void setScores(List<Long> scores) {
  150.         this.scores = scores;
  151.         invalidate();
  152.         this.repaint();
  153.     }
  154.  
  155.     public List<Long> getScores() {
  156.         return scores;
  157.     }
  158.    
  159.     private static void createAndShowGui(List<Long> scores) {
  160.        
  161.         //List<Long> scores = new ArrayList<>();
  162.        // scores = this.getScores();
  163.         Random random = new Random();
  164.         int maxDataPoints = 40;
  165.         int maxScore = 10;
  166.         GraphPanel mainPanel = new GraphPanel(scores);
  167.         mainPanel.setPreferredSize(new Dimension(800, 600));
  168.         JFrame frame = new JFrame("DrawGraph");
  169.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  170.         frame.getContentPane().add(mainPanel);
  171.         frame.pack();
  172.         frame.setLocationRelativeTo(null);
  173.         frame.setVisible(true);
  174.     }
  175.    
  176.     public static void yo(List<Long> scores) {
  177.       SwingUtilities.invokeLater(new Runnable() {
  178.          public void run() {
  179.             createAndShowGui(scores);
  180.          }
  181.       });
  182.    }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement