import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Grapher extends Applet implements ActionListener{
TextField slope;
TextField yintercept;
Button graph;
int sl;
int yinter;
int h1;
int h2;
int v1;
int v2;
Label sloper;
Label yi;
public void init(){
setSize(600,600);
sloper = new Label("Enter Slope");
add(sloper);
slope = new TextField(15);
add(slope);
yi = new Label("Enter Y Intercept");
add(yi);
yintercept = new TextField(15);
add(yintercept);
graph = new Button("Graph");
add(graph);
graph.addActionListener(this);
repaint();
}
public void actionPerformed(ActionEvent e){
sl = Integer.parseInt(slope.getText());
yinter = Integer.parseInt(yintercept.getText());
drawLinear();
}
public void paint( Graphics g )
{ g.setColor( Color.green );
g.drawRect(100,50,500,500);
g.drawLine(350,50,350,550);
g.drawLine(100,300,600,300);
for (int y = 60;y<= 540;y=y+10)
{g.drawLine(345,y,355,y);
}
for (int x = 110;x<= 590;x=x+10)
{g.drawLine(x,295,x,305);
}
g.setColor( Color.red );
g.drawLine(h1,v1,h2,v2);
}
public int convertx(int x4)
{x4 = x4 + 350;
return x4;
}
public int converty(int x4,int s,int y2)
{int y3;
int y = x4 * s + y2;
if (y >= 0)
{y3 = 300 - y;
}
else
{y3 = 300 + Math.abs(y);
}
return y3;
}
public void drawLinear()
{int x1,y1;
boolean started = false;
for (int x = -250;x < 250;x=x+1)
{
x1 = convertx(x);
y1 = converty(x,sl,yinter);
if (!started) // find first point on left
if ((x1 >= 100 && x1<= 600) && (y1 >=50 && y1 <= 550))
{started = true;
h1 = x1;
v1 = y1;
}
if ((x1 >= 100 && x1 <= 600) && (y1 >=50 && y1 <= 550))
{ h2 = x1; // find last point on screeen on right
v2 = y1;
}
repaint();
}
}}