Guest User

Untitled

a guest
May 20th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. package scoutsproject;
  2.  
  3. import java.awt.*;
  4. import java.awt.font.FontRenderContext;
  5. import java.awt.font.LineMetrics;
  6. import java.awt.geom.*;
  7. import javax.swing.*;
  8.  
  9. public class DrawGrowthGraph extends JPanel {
  10.     public int[] data = new int[GuiCheckGroupGrowth.finalGroupSize.length];
  11.     final int PAD = GuiCheckGroupGrowth.finalGroupSize.length;
  12.  
  13.     private void getdata() {
  14.         for(int i = 1; i < GuiCheckGroupGrowth.finalGroupSize.length; i++){
  15.                     data[i] = (int) GuiCheckGroupGrowth.finalGroupSize[i];
  16.                     //System.out.println(data[i]);
  17.                     System.out.println(GuiCheckGroupGrowth.finalGroupSize.length);
  18.                 }
  19.        
  20.     }
  21.  
  22.     protected void paintComponent(Graphics g) {
  23.         getdata();
  24.         super.paintComponent(g);
  25.         Graphics2D g2 = (Graphics2D)g;
  26.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  27.                             RenderingHints.VALUE_ANTIALIAS_ON);
  28.         int w = getWidth();
  29.         int h = getHeight();
  30.         // Draw ordinate.
  31.         g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
  32.         // Draw abcissa.
  33.         g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
  34.         // Draw labels.
  35.         Font font = g2.getFont();
  36.         FontRenderContext frc = g2.getFontRenderContext();
  37.         LineMetrics lm = font.getLineMetrics("0", frc);
  38.         float sh = lm.getAscent() + lm.getDescent();
  39.         // Ordinate label.
  40.         String s = "data";
  41.         float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
  42.         for(int i = 0; i < s.length(); i++) {
  43.             String letter = String.valueOf(s.charAt(i));
  44.             float sw = (float)font.getStringBounds(letter, frc).getWidth();
  45.             float sx = (PAD - sw)/2;
  46.             g2.drawString(letter, sx, sy);
  47.             sy += sh;
  48.         }
  49.         // Abcissa label.
  50.         s = "x axis";
  51.         sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
  52.         float sw = (float)font.getStringBounds(s, frc).getWidth();
  53.         float sx = (w - sw)/2;
  54.         g2.drawString(s, sx, sy);
  55.         // Draw lines.
  56.         double xInc = (double)(w - 2*PAD)/(data.length-1);
  57.         double scale = (double)(h - 2*PAD)/getMax();
  58.         g2.setPaint(Color.green.darker());
  59.         for(int i = 0; i < data.length-1; i++) {
  60.             double x1 = PAD + i*xInc;
  61.             double y1 = h - PAD - scale*data[i];
  62.             double x2 = PAD + (i+1)*xInc;
  63.             double y2 = h - PAD - scale*data[i+1];
  64.             g2.draw(new Line2D.Double(x1, y1, x2, y2));
  65.         }
  66.         // Mark data points.
  67.         g2.setPaint(Color.red);
  68.         for(int i = 0; i < data.length; i++) {
  69.             double x = PAD + i*xInc;
  70.             double y = h - PAD - scale*data[i];
  71.             g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
  72.         }
  73.     }
  74.  
  75.     private int getMax() {
  76.         int max = -Integer.MAX_VALUE;
  77.         for(int i = 0; i < data.length; i++) {
  78.             if(data[i] > max)
  79.                 max = data[i];
  80.         }
  81.         return max;
  82.     }
  83.    
  84.    
  85.     public void makeWindow() {
  86.                
  87.         JFrame f = new JFrame();
  88.         f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  89.         f.add(this);
  90.         f.setSize(400, 400);
  91.         f.setLocation(200, 200);
  92.         f.setVisible(true);
  93.     }
  94.  
  95.     public static void main(String[] args) {
  96.  
  97.        
  98.     }
  99.  
  100. }
Add Comment
Please, Sign In to add comment