Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.turt2live.grapher;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.image.BufferedImage;
- import java.util.Vector;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Graph extends JPanel {
- private static final long serialVersionUID = 1L;
- @SuppressWarnings("unused")
- private JFrame window;
- public Graph(JFrame w) {
- window = w;
- }
- @Override
- public void paintComponent(Graphics go) {
- Graphics2D g = (Graphics2D) go;
- g.drawImage(getGraph(), 0, 0, null);
- }
- private BufferedImage getGraph() {
- int scaleX[] = { 0, 10, 20, 30, 40, 50 };
- int scaleY[] = { 0, 20, 40, 60, 80, 100 };
- double dataX[] = { 5, 10, 15, 20, 25, 30 };
- double dataY[] = { 100, 80, 60, 40, 20, 0 };
- String title = "Test Graph";
- int gpx[] = { 40, 720 };
- int gpy[] = { 80, 180 };
- BufferedImage image = new BufferedImage(800, 300, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g = (Graphics2D) image.getGraphics();
- g.setColor(Color.GRAY);
- g.fillRect(0, 0, 800, 300);
- g.setColor(Color.DARK_GRAY);
- g.fillRect(10, 10, 780, 280);
- g.setColor(Color.GRAY);
- g.fillRect(20, 20, 760, 35);
- g.setColor(Color.BLACK);
- g.setFont(new Font("Arial", Font.PLAIN, 26));
- g.drawString(title, 25, 45);
- g.setColor(Color.GRAY);
- g.fillRect(20, 60, 760, 220);
- g.setColor(Color.LIGHT_GRAY);
- g.fillRect(40, 80, 720, 180);
- // Draw X-Scale (Also draws lines)
- g.setColor(Color.BLACK);
- g.setFont(new Font("Arial", Font.PLAIN, 14));
- int sx = (gpx[1] - gpx[0]) / (scaleX.length - 1);
- for (int i = 0; i < scaleX.length; i++) {
- String label = scaleX[i] + "";
- g.drawString(label, gpx[0] + (sx * i) + 5, gpy[1] + 95);
- g.drawLine(gpx[0] + (sx * i) + 10, gpy[1] + 80, gpx[0] + (sx * i) + 10, gpy[0]);
- }
- // Draw Y-Scale (Also draws lines)
- g.setColor(Color.BLACK);
- g.setFont(new Font("Arial", Font.PLAIN, 14));
- int sy = ((gpy[1] - gpy[0]) / (scaleY.length - 1)) + 14;
- for (int i = 0; i < scaleY.length; i++) {
- String label = scaleY[i] + "";
- g.drawString(label, gpx[0] - 20, gpy[1] - (sy * i) + 80);
- g.drawLine(gpx[0], gpy[1] - (sy * i) + 75, gpx[1] + 40, gpy[1] - (sy * i) + 75);
- }
- // Find all points
- Vector<Point> points = new Vector<Point>();
- for (int i = 0; i < ((dataX.length <= dataY.length) ? dataX.length : dataY.length); i++) {
- double x = 0;
- double y = 0;
- double msx = scaleX[0];
- double msy = scaleY[0];
- double masx = scaleX[scaleX.length - 1];
- double masy = scaleY[scaleY.length - 1];
- x = ((dataX[i] - msx) / (masx - msx));
- double nx = (x * gpx[1]) + msx;
- y = ((dataY[i] - msy) / (masy - msy));
- y = 1 - y;
- double ny = (y * gpy[1]) + msy;
- //System.out.println("(" + dataX[i] + ", " + dataY[i] + ") | (" + nx + ", " + ny + ") | (" + x + ", " + y + ")");
- points.add(new Point((int) (nx + 38), (int) (ny + 78)));
- }
- //System.out.println("------------");
- // Draw points (as a line graph)
- g.setColor(Color.CYAN);
- g.setStroke(new BasicStroke(3F));
- for (int i = 0; i < points.size() - 1; i++)
- g.drawLine(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y);
- return image;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment