I have a school assignment for CompSci where we need to create a graph for linear equations. This graph should have the axes drawn, and it should have two lines that are drawn from two equations. I have the graph itself working correctly, but I can't seem to figure out how to set the coordinates of the lines right. Currently I am using the following code to create and send the coordinates to my graph:
m.print("Slope as decimal: ");
//gets slope of line. This will be changed so it will accept fractions like Rise/Run but not until the rest of the functionality works.
double lslope = m.readDouble();
//gets y intercept as B
m.print("B as decimal: ");
double lb = m.readDouble();
//corresponds to the first coordinate
double x11=0;
double y11 = lslope*x11-lb*50;
//corresponds to the second coordinate
double x21=1000;
double y21 = lslope*x21-lb*50;
//outputs the coordinates of the two points just for the user's conveinience
m.println(""+x11+"\n"+y11+"\n"+x21+"\n"+y21);
//wingraph is a JFrame
wingraph graph = new wingraph("Graph");
graph.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
graph.setSize(1000,1000);
graph.setResizable(false);
//the second set of 4 numbers will be a second line, but to get the graph working I'm just focusing on using one set. The setCoords method just sets the value of 8 coordinates inside a JFrame class which paints the X and Y axis in black, and then paints two lines with the specified coordinates in red.
graph.setCoords(x11, y11, x21, y21, 1000,1000,1000,1000);
//displays the graph
graph.setVisible(true);
By inverting the slope the user entered, I can get the line to work correctly, but only if lslope
is equal to 1. How could I manipulate the integers correctly so that my graph is displayed well whether the slope has decimals, is any real number, or is positive or negative?
If it helps, the code for the JFrame I'm using is:
public class wingraph extends JFrame{
public wingraph(String title){
super(title);
setLayout(new FlowLayout());
}
double xCoord1 = 0, yCoord1 = 0, xCoord2 = 0, yCoord2 = 0, xCoord12 = 0, yCoord12 = 0, xCoord22 = 0, yCoord22 = 0;
public void setCoords(double x1, double y1, double x2, double y2, double x12, double y12, double x22, double y22){
xCoord1 = x1;
yCoord1 = y1;
xCoord2 = x2;
yCoord2 = y2;
xCoord12 = x12;
yCoord12 = y12;
xCoord22 = x22;
yCoord22 = y22;
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLACK);
g.drawLine(this.getWidth()/2, 0, this.getWidth()/2, this.getHeight());
g.drawLine(0, this.getHeight()/2, this.getWidth(), this.getHeight()/2);
g.drawString("0",this.getWidth()/2-10,this.getHeight()/2-5);
g.setColor(Color.red);
g.drawLine((int) xCoord1,(int) yCoord1,(int) xCoord2,(int) yCoord2);
g.drawLine((int) xCoord12,(int) yCoord12,(int) xCoord22,(int) yCoord22);
}
}
Any help is appreciated.
Edit
Examples of my code running is this:
These all have issues since I need them all to be centered on the axis and the correct format such as:
I apologize if this is still unclear, but I am new to Java and am not all that familiar with how to explain exactly what I want other than to graph 2 lines with the equation y = mx + b
on a Cartesian plane.
Edit 2
If the user inputs 1 for the lslope
and 0 for b
, I want the x and y variables to be:
x11 = 0;
y11 = 1000;
x21 = 1000;
y21 = 0;
If lslope = 2
and b = 5
I want the x and y variables to be:
x11 = 0
y11 = 745;
x21 = 1000;
y21 = 245;
If lslope = -1
and b = 10
I want the x and y variables to be:
x11 = 0
y11 = 240;
x21 = 1000;
y21 = 740;