# How do you graph a simple line with user inputed slope and intercept?

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 12-26-2011, 05:51 PM
only1zster
How do you graph a simple line with user inputed slope and intercept?
First, I would appreciate any and all help with this.

I've written the code to get the user inputed slope (m) and intercept (b). That part appears to be fine. I am however having a hard time converting that information correctly into a line correctly displayed on a graph.
I've set the field to a specific size to help me with this to no avail.

Code:

```public class graphAttempt extends JPanel {         //create string variables         private String slopeValue;         private String interceptValue;                 private double m; //Slope of the line         private double b; //Intercept (where the line intercepts y         private double x1 = -195;         private double x2 = 195;         private double y1;         private double y2;         final int center = 195;         final int PAD = 20;//for this purpose it's the padding from the left side, and bottom on the graph         private void getSlope()         {                 boolean done = false;                                                 slopeValue = JOptionPane.showInputDialog(null,                                         "Enter in the slope of the line you wish to graph :");                                 try                                 {                                         if(slopeValue==null) throw new NullPointerException();                                         this.m = Double.parseDouble(slopeValue);                                         this.m = this.m + this.center;                                                                                                         }                                 catch (NullPointerException e)                                 {                                         JOptionPane.showMessageDialog(null,                                                         "Please enter in a number! ", "Input Error",                                                         JOptionPane.ERROR_MESSAGE);                                 }                                 catch (InputMismatchException e)                                 {                                         JOptionPane.showMessageDialog(null,                                                         "Please enter in a number! ", "Input Error",                                                         JOptionPane.ERROR_MESSAGE);                                 }                                 done = true;         }         private void getIntercept()         {                 boolean done = false;                                                 interceptValue = JOptionPane.showInputDialog(null,                                         "Enter in the intercept of the line you wish to graph :");                                 try                                 {                                         if(interceptValue==null) throw new NullPointerException();                                         this.b = Double.parseDouble(interceptValue);                                         this.b = this.b + this.center;                                                                }                                 catch (NullPointerException e)                                 {                                         JOptionPane.showMessageDialog(null,                                                         "Please enter in a number! ", "Input Error",                                                         JOptionPane.ERROR_MESSAGE);                                 }                                 catch (InputMismatchException e)                                 {                                         JOptionPane.showMessageDialog(null,                                                         "Please enter in a number! ", "Input Error",                                                         JOptionPane.ERROR_MESSAGE);                                 }                                 done = true;         }         private void calculateY()         {                                 this.y1 = (this.m*(this.x1)) + this.b;                 this.y2 = (this.m*(this.x2)) + this.b;         }                 protected void paintComponent(Graphics g)         {                 Graphics2D g2 = (Graphics2D)g;                                 int w = 400;                 int h = 400;                                                 g2.setPaint(Color.gray);                 g2.draw(new Line2D.Double(.1*w, PAD, .1*w, h-PAD));//-4 y axis                 g2.draw(new Line2D.Double(.2*w, PAD, .2*w, h-PAD));//-3 y axis                 g2.draw(new Line2D.Double(.3*w, PAD, .3*w, h-PAD));//-2 y axis                 g2.draw(new Line2D.Double(.4*w, PAD, .4*w, h-PAD));//-1 y axis                 g2.setPaint(Color.black);                 g2.draw(new Line2D.Double(.5*w, PAD, .5*w, h-PAD));//0 y axis                 g2.setPaint(Color.gray);                 g2.draw(new Line2D.Double(.6*w, PAD, .6*w, h-PAD));//1 y axis                 g2.draw(new Line2D.Double(.7*w, PAD, .7*w, h-PAD));//2 y axis                 g2.draw(new Line2D.Double(.8*w, PAD, .8*w, h-PAD));//3 y axis                 g2.draw(new Line2D.Double(.9*w, PAD, .9*w, h-PAD));//4 y axis                                                 g2.draw(new Line2D.Double(PAD, .1*h, w-PAD, .1*h));//+4 x axis                 g2.draw(new Line2D.Double(PAD, .2*h, w-PAD, .2*h));//+3 x axis                 g2.draw(new Line2D.Double(PAD, .3*h, w-PAD, .3*h));//+2 x axis                 g2.draw(new Line2D.Double(PAD, .4*h, w-PAD, .4*h));//+1 x axis                 g2.setPaint(Color.black);                 g2.draw(new Line2D.Double(PAD, .5*h, w-PAD, .5*h));//0 x axis                 g2.setPaint(Color.gray);                 g2.draw(new Line2D.Double(PAD, .6*h, w-PAD, .6*h));//-1 x axis                 g2.draw(new Line2D.Double(PAD, .7*h, w-PAD, .7*h));//-2 x axis                 g2.draw(new Line2D.Double(PAD, .8*h, w-PAD, .8*h));//-3 x axis                 g2.draw(new Line2D.Double(PAD, .9*h, w-PAD, .9*h));//-4 x axis                                 g2.setPaint(Color.red);                 g2.draw(new Line2D.Double(this.center*(x1*.1),this.center*(y1*.1),this.center+x2, this.center+y2));                         }                         public static void main(String[] args)         {                 graphAttempt grph = new graphAttempt();                 grph.getSlope();                 grph.getIntercept();                 grph.calculateY();                                 JFrame f = new JFrame();                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 f.add(new graphAttempt());                 f.setSize(450,450);//sets the size of the window                 f.setLocation(200,200); //sets the location 200,200 of the window from the upper left corner of the screen                 f.setVisible(true);         } }```
• 12-26-2011, 06:10 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
Can you describe what the program does and why that is wrong and explain what it should do?

For easier debugging, can you change the program, comment out the user prompts and hard code values for testing.
For example:
slopeValue = 23; // JOptionPane.showInputDialog(null,

Then we can all be talking about the same program with the same data.
• 12-26-2011, 07:11 PM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
I commented out both inputs, and assigned this.m = 1, and this.b=3.

The program displays a nice grid, who's center point is 195(x),195(y). Each intersection is in increments of 40 (ie 35, 75, 115, 135, 195...).
Given the formula y=m(x)+b, and the user inputs of m (slope) and b (intercept) the program is supposed to correctly plot the line (x1, y1, x2, y2), but I don't know where I'm going wrong in converting the inputted values for m and b into the correct plotting points.

I understand that b will be where the sloping line will intersect y at the 0x axis. So if a user were to input a slope of 1, and an intercept of 3, the plotted line would go from -3y,0x up to 0y,3x.
• 12-26-2011, 07:14 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
You need to do some debugging on the code to check that your math is correct.
Add lots of printlns to show the values of ALL of the variables as they change.

I changed the source:
Code:

```        private double m = 1; //Slope of the line         private double b = 3; //Intercept (where the line intercepts y   .. and in main() //                grph.getSlope(); //                grph.getIntercept();```
and get this image:
• 12-26-2011, 10:42 PM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
I assigned the variables as you did, and changed the main, but this is the graph that I get.
I do understand that the math somewhere is screwed up but I don't know where to begin.
Attachment 2413
• 12-26-2011, 10:48 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
You must have changed something else. I do not have any code that draws a blue circle.

Add some printlns to show the values of the variables. Print them out in calculate() and print out the x,y values for the line that is drawn in the paint method.
• 12-26-2011, 11:02 PM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
I put the blue circle in there to find the x,y value of the center. thinking I need to do the math in relationship to that point (or values)
Values before drawing the graph and line: b: 3.0, m: 1.0, x1: -200.0, x2: 200.0, y1: 203.0, y2: 203.0

The code do draw the line. (I've done straight x1 and this.x1 for all variables with the same results)
g2.setPaint(Color.red);
g2.draw(new Line2D.Double(x1,y1,x2,y2));

Value of the variables after drawing the graph and line: x1: -200.0, y1: 0.0, x2: 200.0, y2: 0.0

somewhere the values are getting reset.
• 12-26-2011, 11:08 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
What would cause the values to be reset? What does your code do that could cause new values for the variables? Look very closely at the main method.
• 12-26-2011, 11:17 PM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
I looked at the code, and walked through it. Right now, it initializes the variables, then calculates the values of Y. Then it graphs it. I don't see any place where it would change the values.
graphAttempt grph = new graphAttempt();
//grph.getSlope();
//grph.getIntercept();
grph.calculateY();

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(450,450);//sets the size of the window
f.setLocation(200,200); //sets the location 200,200 of the window from the upper left corner of the screen
f.setVisible(true);
• 12-26-2011, 11:18 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
What if you create a second object?
• 12-26-2011, 11:21 PM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
not sure what you mean.
• 12-26-2011, 11:24 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
To see what is happening add a constructor to the graphAttempt class. Add a println statement to it that prints out a message saying a new object was created, Compile and execute the program and see what is printed out.
• 12-26-2011, 11:42 PM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
I changed the variables at the beginning to: private static double ___ and that worked in keeping the values as they were assigned within the code. Now the math just needs to be fixed so that it will plot correctly.
I really do appreciate all your help.
Attachment 2414
• 12-26-2011, 11:45 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
static is not the solution. What if you want two graphs in the same window?
Did you create the constructor with println and execute the program?
What was printed out?
• 12-26-2011, 11:51 PM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
I guess I don't understand what I'm supposed to do.
I copied the main method and changed all the grph to grph2. along with the println's for the variables. it gave me the error that the variables needed to be static, so I changed them, and it ran fine. none of the variables reset.
• 12-26-2011, 11:56 PM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
Do you know how to write a class's constructor? add a constructor to the graphAttempt class
Do you know how to code a println statement? Add a println statement to that constructor that prints out a message

The purpose of this is to show you what your code is doing.
• 12-27-2011, 12:04 AM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
no, and yes (System.out.println("text" + variable);
• 12-27-2011, 12:08 AM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
Constructors are so common that I don't know how you missed them.
Almost all classes are written with constructors.
http://docs.oracle.com/javase/tutori...structors.html

???? Here is a sample:
Code:

```public graphAttempt() {   System.out.println("in graphAttempt constructor"); }```
• 12-27-2011, 12:40 AM
only1zster
Re: How do you graph a simple line with user inputed slope and intercept?
As for constructors, it may have been discussed, but only briefly. I honestly don't remember them.
I deleted the static portion from the variable initializing and put the println in several different area's in the code. The println's all showed the variables being reset.
• 12-27-2011, 12:44 AM
Norm
Re: How do you graph a simple line with user inputed slope and intercept?
Did you add the constructor I asked you to put in the code with the println as I showed?
Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last