Graphing in JAVA for Final Project

ScottALot

Active Member
Hey guys, I'm having an issue learning the graphing functions of JAVA for a final project. My project is to create a Calculus Suite. Most of the raw calculation stuff is going just fine, but I can't figure out how to graph correctly. I did some research and found some sample code, but it's specific to certain points... but it has a structure that seems simple enough, I just can't figure out what certain things do.

My intention is for the user to plug an algebraic equation into the code, compile it, and then run it to get a graph. I chose this somewhat cumbersome way because (as far as I can tell) there is no way for a user to input an equation any other way.

Here's that sample code I found online:

Code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
 
public class GraphingData extends JPanel {
    int[] data = {
        21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
        61, 55, 48, 60, 49, 36, 38, 27, 20, 18
    };
    final int PAD = 20;
 
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth();
        int h = getHeight();
        // Draw ordinate.
        g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
        // Draw abcissa.
        g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
        double xInc = (double)(w - 2*PAD)/(data.length-1);
        double scale = (double)(h - 2*PAD)/getMax();
        // Mark data points.
        g2.setPaint(Color.red);
        for(int i = 0; i < data.length; i++) {
            double x = PAD + i*xInc;
            double y = h - PAD - scale*data[i];
            g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
        }
    }
 
    private int getMax() {
        int max = -Integer.MAX_VALUE;
        for(int i = 0; i < data.length; i++) {
            if(data[i] > max)
                max = data[i];
        }
        return max;
    }
 
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new GraphingData());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

I pretty much understand nothing going on here... can someone comment most of these lines? It'd be greatly appreciated... the online JAVA library doesn't seem to help.
 
Oh! Let me try! Let's see....

Code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
 
public class GraphingData extends JPanel {
        
        int[] data = {
        21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
        61, 55, 48, 60, 49, 36, 38, 27, 20, 18
    };
    // A padding size between data points
    // so the graph isn't so cramped
    final int PAD = 20;
 
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //Creates new Graphics2D object
        // Used to draw graphics
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth();
        int h = getHeight();
        // Draw ordinate.
        // Draws a straight line; arguments are x1, y1, x2, y2
        g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
        // Draw abcissa.
        // Another line
        g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
        // These are the rate at which the lines increment (or increase)
        // as well as scale, in accordance to the given data
        double xInc = (double)(w - 2*PAD)/(data.length-1);
        double scale = (double)(h - 2*PAD)/getMax();
        // Mark data points.
        // Marks the data points in red
        g2.setPaint(Color.red);
        for(int i = 0; i < data.length; i++) {
            double x = PAD + i*xInc;
            double y = h - PAD - scale*data[i];
            //draws an ellipse; arguments are x, y, width, height
            g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
        }
    }
 
    // Just finds the largest value in the data array above
    private int getMax() {
        int max = -Integer.MAX_VALUE;
        for(int i = 0; i < data.length; i++) {
            if(data[i] > max)
                max = data[i];
        }
        return max;
    }
 
    //Initializes everything and runs the program.
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new GraphingData());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

Edit: Had a couple parameters mixed up. FIXED.
 
Last edited:
Thanks man! Very much appreciated. I'm making some good head way on that... I'm changing up the program so that it also reports LRAM, RRAM, and TRAPezoid Integral Approximations (if you took Calc, you know what I'm talking about). I'll put the finished code up here later.
 
Back
Top