Graph inputted values

Hey everyone,

I'm writing a program that takes the derivative of an equation and outputs the derivative and shows a graph with the original equation and the derivative. How would I go about making a graph show up in the panel, and then draw the lines. Im confused at this part. I can get the input working, but how do I make a graph and lines and such.

[366 byte] By [bcleva] at [2007-11-27 6:46:56]
# 1
JPanel panel = new JPanel(){public void paintComponent(Graphics g){//do all your drawing hereg.drawLine(10,10,300,200);}}
Nethera at 2007-7-12 18:19:23 > top of Java-index,Java Essentials,Java Programming...
# 2
REPLY #20 http://forum.java.sun.com/thread.jspa?threadID=5123534&start=15&tstart=0
TuringPesta at 2007-7-12 18:19:23 > top of Java-index,Java Essentials,Java Programming...
# 3
I can draw lines, but what about parabolas, x^3 and so on
bcleva at 2007-7-12 18:19:23 > top of Java-index,Java Essentials,Java Programming...
# 4

Using

import java.applet.*;

import java.awt.*;

import java.awt.geom.*;

public class Graph2D extends Applet{

public void init(){

this.setBackground(Color.LIGHT_GRAY);

}

public void paint(Graphics g){

Graphics2D gfx = (Graphics2D)g;

gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

gfx.setColor(Color.BLACK);

Xscale = (double)this.getWidth() / (Xend - Xstart);

Yscale = (double)this.getHeight() / (Yend - Ystart);

double iterScale = (Xend - Xstart) / (double)this.getWidth();

double fx;

convertToScreen(start, Xstart, function(Xstart));

for(double x = Xstart; x <= Xend; x += iterScale){

fx = function(x);

convertToScreen(end, x, fx);

line.setLine(start, end);

gfx.draw(line);

start.setLocation(end.getX(), end.getY());

}

}

public double function(double x){

return Math.sin(x);

}

public void convertToScreen(Point2D pt, double x, double y){

double px = ((x - Xstart) * Xscale);

double py = this.getHeight() - (((y - Ystart) * Yscale));

pt.setLocation(px, py);

}

double Xstart = -30;

double Xend = 30;

double Xscale;

double Ystart = -2;

double Yend = 2;

double Yscale;

Point2D start = new Point2D.Double();

Point2D end = new Point2D.Double();

Line2D.Double line = new Line2D.Double();

}

How would I graph x^2 and such? I tried replacing Math.sin(x) with x*x, but it doesnt work.

bcleva at 2007-7-12 18:19:23 > top of Java-index,Java Essentials,Java Programming...
# 5
well im assuming you dont want an Applet.you do have to do SOME work, lol.change the Applet to a JFrame & change paint() to paintComponent()and add super.paintComponent(g)... etc etc.
TuringPesta at 2007-7-12 18:19:23 > top of Java-index,Java Essentials,Java Programming...
# 6
What I want is that when I press the JButton "derive", a graph pops up with two functions. I dont understand how to convert from an applet...
bcleva at 2007-7-12 18:19:23 > top of Java-index,Java Essentials,Java Programming...