turt2live

Untitled

Dec 22nd, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package me.turt2live.grapher;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.Point;
  9. import java.awt.image.BufferedImage;
  10. import java.util.Vector;
  11.  
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14.  
  15. public class Graph extends JPanel {
  16.  
  17.     private static final long   serialVersionUID    = 1L;
  18.  
  19.     @SuppressWarnings("unused")
  20.     private JFrame              window;
  21.  
  22.     public Graph(JFrame w) {
  23.         window = w;
  24.     }
  25.  
  26.     @Override
  27.     public void paintComponent(Graphics go) {
  28.         Graphics2D g = (Graphics2D) go;
  29.         g.drawImage(getGraph(), 0, 0, null);
  30.     }
  31.  
  32.     private BufferedImage getGraph() {
  33.         int scaleX[] = { 0, 10, 20, 30, 40, 50 };
  34.         int scaleY[] = { 0, 20, 40, 60, 80, 100 };
  35.         double dataX[] = { 5, 10, 15, 20, 25, 30 };
  36.         double dataY[] = { 100, 80, 60, 40, 20, 0 };
  37.         String title = "Test Graph";
  38.         int gpx[] = { 40, 720 };
  39.         int gpy[] = { 80, 180 };
  40.  
  41.         BufferedImage image = new BufferedImage(800, 300, BufferedImage.TYPE_INT_ARGB);
  42.         Graphics2D g = (Graphics2D) image.getGraphics();
  43.         g.setColor(Color.GRAY);
  44.         g.fillRect(0, 0, 800, 300);
  45.         g.setColor(Color.DARK_GRAY);
  46.         g.fillRect(10, 10, 780, 280);
  47.         g.setColor(Color.GRAY);
  48.         g.fillRect(20, 20, 760, 35);
  49.         g.setColor(Color.BLACK);
  50.         g.setFont(new Font("Arial", Font.PLAIN, 26));
  51.         g.drawString(title, 25, 45);
  52.         g.setColor(Color.GRAY);
  53.         g.fillRect(20, 60, 760, 220);
  54.         g.setColor(Color.LIGHT_GRAY);
  55.         g.fillRect(40, 80, 720, 180);
  56.         // Draw X-Scale (Also draws lines)
  57.         g.setColor(Color.BLACK);
  58.         g.setFont(new Font("Arial", Font.PLAIN, 14));
  59.         int sx = (gpx[1] - gpx[0]) / (scaleX.length - 1);
  60.         for (int i = 0; i < scaleX.length; i++) {
  61.             String label = scaleX[i] + "";
  62.             g.drawString(label, gpx[0] + (sx * i) + 5, gpy[1] + 95);
  63.             g.drawLine(gpx[0] + (sx * i) + 10, gpy[1] + 80, gpx[0] + (sx * i) + 10, gpy[0]);
  64.         }
  65.         // Draw Y-Scale (Also draws lines)
  66.         g.setColor(Color.BLACK);
  67.         g.setFont(new Font("Arial", Font.PLAIN, 14));
  68.         int sy = ((gpy[1] - gpy[0]) / (scaleY.length - 1)) + 14;
  69.         for (int i = 0; i < scaleY.length; i++) {
  70.             String label = scaleY[i] + "";
  71.             g.drawString(label, gpx[0] - 20, gpy[1] - (sy * i) + 80);
  72.             g.drawLine(gpx[0], gpy[1] - (sy * i) + 75, gpx[1] + 40, gpy[1] - (sy * i) + 75);
  73.         }
  74.         // Find all points
  75.         Vector<Point> points = new Vector<Point>();
  76.         for (int i = 0; i < ((dataX.length <= dataY.length) ? dataX.length : dataY.length); i++) {
  77.             double x = 0;
  78.             double y = 0;
  79.             double msx = scaleX[0];
  80.             double msy = scaleY[0];
  81.             double masx = scaleX[scaleX.length - 1];
  82.             double masy = scaleY[scaleY.length - 1];
  83.             x = ((dataX[i] - msx) / (masx - msx));
  84.             double nx = (x * gpx[1]) + msx;
  85.             y = ((dataY[i] - msy) / (masy - msy));
  86.             y = 1 - y;
  87.             double ny = (y * gpy[1]) + msy;
  88.             //System.out.println("(" + dataX[i] + ", " + dataY[i] + ") | (" + nx + ", " + ny + ") | (" + x + ", " + y + ")");
  89.             points.add(new Point((int) (nx + 38), (int) (ny + 78)));
  90.         }
  91.         //System.out.println("------------");
  92.         // Draw points (as a line graph)
  93.         g.setColor(Color.CYAN);
  94.         g.setStroke(new BasicStroke(3F));
  95.         for (int i = 0; i < points.size() - 1; i++)
  96.             g.drawLine(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y);
  97.         return image;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment