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:
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.
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.