JAVA Create an applet/GUI to draw a graph of a function gine in cartesian or par
ID: 660681 • Letter: J
Question
JAVA
Create an applet/GUI to draw a graph of a function gine in cartesian or parametric cordinates. You need to draw n line segments joining the successive points (x, f(x)) and (x+d, f(x+d)) (or two points (x(t), y(t)) and (x(t+d)y(t+d)) if using parametric coordinates). Experiment with the values of n = 10,25,50,100,200 to make the best choice. Draw the graphs of the following three functions:
a) f(x) = x3 + x - 3 on the interval [0,4]
b) g(x) = x3 /100 - x + 10 on the interval [-10,10]
c) "The four-leaved rose" whose equation in polar coordinates is r=cos2x with 0 < x < 2pi
any source code or pseucode would help
Explanation / Answer
import java.awt.*;
import java.applet.*;
public class plot_function extends Applet
{
int txmi,tymi,txma,tyma;
double xmi,ymi,xma,yma;
double deltax;
public void init(){
xmi=-8;xma=8;ymi=-5;yma=5;
txmi=0;txma=500;tymi=0;tyma=500;
deltax=0.1;
}
public void paint(Graphics g1){
double a,b,c,d;
//set the backgroud color
g1.setColor(new Color(0, 150, 180));
g1.fillRect(txmi,tymi,txma-txmi,tyma-tymi);
// creating the coordinates
g1.setColor(Color.green);
Line(g1,0,ymi,0,yma);
Line(g,xmi,0,xma,0);
//drawing the graph
g1.setColor(Color.black);
x2=xmi;y2=funct(x2);
for(x1=xmi;x1<=xma;x1=x1+deltax)
{
y1=funct(x1);
Line(g1,x1,y1,x2,y2);
x2=x1;y2=y1;
}
}
public int createmapX(double x){
int sx;
sx= txmi+(int)((x-xmi)/(xma-xmi)*(double)(txma-txmi)) ;
return sx;
}
public int createmapY(double y){
int sy;
sy=tymi+(int)((yma-y)/(yma-ymi)*(double)(tyma-tymi));
return sy;
}
public void Line(Graphics g1,double x1,double y1,double x2,double y2){
g1.drawLine(createmapX(x1),createmapY(y1),createmapX(x2),createmapY(y2));
}
public double funct(double x){
double y;
y=x*x;
return y;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.