import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import java.awt.Color;
public class gr03_draw extends JPanel {
private int []coEffi; // array for coefficients
public gr03_draw(int []userChoice) // takes an array filled with coefficients
{
coEffi=userChoice; //assigns the array to another one
}
public void paintComponent(Graphics gx){
super.paintComponent(gx);
Graphics2D g=(Graphics2D)gx;
int width= getWidth();
int height= getHeight();
g.setColor(Color.WHITE); //setting background color
g.fillRect(0, 0, width, height);
g.setColor(Color.RED);
g.drawLine(0, height/2, width, height/2); // x-axis line
g.drawLine(width/2, 0, width/2, height); //y-axis line
int degree=coEffi.length-1;
for(int x=-width; x<=width; x++) //finds the ordinates from given values of x
{
int y=0;
int y2=0;
for(int i=0; i<=degree; i++) //finds the y1 for the line
{
y+=(coEffi[i]*Math.pow(x,i));
}
for(int i2=0; i2<=degree; i2++) //finds the y2 for the line
{
y2+=(coEffi[i2]*Math.pow(x+1,i2));
}
/*Following three lines are used to hold origin at the center I am not using "translate" here, as I was unable to set the direction of y-axis from*/
int pntX=(width/2)+x;
int pntY=height-((height/2)+y);
int pntY2=height-((height/2)+y2);
//it draws the lines
g.drawLine(pntX, pntY, pntX+1, pntY2);
}//end of loop
}
}