Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48
  1. #21
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    yes, exactly as you showed.

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Then what happened?

  3. #23
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    it showed that all the variables had reset to the initialized values, no matter where I put the constructor in the code. (Directly after the variables were initialized, before the calculateY() method, after the calculateY() method, after the paintComponent(Graphics g).
    in Week5IndiviudalAssignment constructor
    x1: -200.0, y1: 0.0, x2: 200.0, y2: 0.0

    The only times that the variables showed they had been changed was within the calculateY() method.
    Value of B: 3.0
    Value of M: 1.0
    Value of x1: -200.0
    Value of x2: 200.0
    Value of y1: 203.0
    Value of y2: 203.0

  4. #24
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    System.out.println("in graphAttempt constructor");
    How many times was the output from this line shown?
    How many times did you expect this line to execute?

  5. #25
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    it outputed once. that is all I expected it to.

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Have you changed your main() method since post #9?

  7. #27
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    No I haven't.

  8. #28
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Something about your code is different from what has been posted here.
    Your code creates two instances of the graphAttempt object which means there should be two print outs from the constructor. But you say there is only one. Why is that? What is different about your code?

  9. #29
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.util.InputMismatchException;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    
    public class graphAttempt extends JPanel
    {
    	//create string variables
    	private String slopeValue;
    	private String interceptValue;
    	
    	private double m = 1; //Slope of the line
    	private double b = 3; //Intercept (where the line intercepts y
    	private double x1 = -200;
    	private double x2 = 200;
    	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
    
    	public graphAttempt()
    	{
    		System.out.println("in Week5IndiviudalAssignment constructor");
    		System.out.println("x1: " + this.x1 + 
    				   ", y1: " + this.y1 +
    				   ", x2: " + this.x2 +
    				   ", y2: " + this.y2);
    		System.out.println("out Week5IndividualAssignment contructor");
    	}
    	private void getSlope()...
    
    	private void getIntercept()...
    
    	private void calculateY()
    	{
    		
    
    		System.out.println("Value of B: " + b);
    		System.out.println("Value of M: " + m);
    		System.out.println("Value of x1: " + x1);
    		System.out.println("Value of x2: " + x2);
    		
    		y1= (m * -x1) + b;
    		y2= (m * x2) + b;
    
    		System.out.println("Value of y1: " + y1);
    		System.out.println("Value of y2: " + y2);
    		//System.out.println(this.m);
    		//System.out.println(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.x1,this.y1,this.x2,this.y2));
    		System.out.println("x1: " + this.x1 + 
    						   ", y1: " + this.y1 +
    						   ", x2: " + this.x2 +
    						   ", y2: " + this.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);
    		
    
    		
    	}
    
    }
    Last edited by Norm; 12-27-2011 at 01:57 AM. Reason: added code tags

  10. #30
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Here's what I get when I execute your code from post #29
    Running: F:\Java\jre6\bin\java.exe -classpath D:\JavaDevelopment;.;..\. -Xmx512M GraphAttempt2

    in Week5IndiviudalAssignment constructor
    x1: -200.0, y1: 0.0, x2: 200.0, y2: 0.0
    out Week5IndividualAssignment contructor
    Value of B: 3.0
    Value of M: 1.0
    Value of x1: -200.0
    Value of x2: 200.0
    Value of y1: 203.0
    Value of y2: 203.0
    in Week5IndiviudalAssignment constructor
    x1: -200.0, y1: 0.0, x2: 200.0, y2: 0.0
    out Week5IndividualAssignment contructor
    x1: -200.0, y1: 0.0, x2: 200.0, y2: 0.0

    0 error(s)
    The lines of interest are in red. They show that the constructor was executed TWO TIMES!!!!!!!

    What does your response in post #25 refer to????

  11. #31
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    I'm sorry, when I ran it I didn't notice that there was more information above "Value of B:.....". I expected the output to only run once.

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    I expected the output to only run once.
    Famous last words.

  13. #33
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    So I take it that somehow the program is re-initializing all the variables after each method?

  14. #34
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    The issue is WHY are there two instances of the graphAttempt object being created.
    You call the calculate() method for the first instance.
    You do NOT call the calculate method for the second one.

    If you can not see where the instances are being create, add this to the graphAttempt constructor:
    Thread.dumpStack(); // show the call stack

    The print out will show who and where the call to the constructor was made.

  15. #35
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    it showed f.add(new graphAttempt());

  16. #36
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Yes, that is the place the second instance was created.
    Why create a second instance? Why not use the first one?

  17. #37
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    I really am sorry that your spending so much time helping me with this. I do appreciate it though.

    I don't see how to remove:
    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);
    and consolidate it into the first:
    graphAttempt grph = new graphAttempt();
    //grph.getSlope();
    //grph.getIntercept();
    grph.calculateY();

    I first commented the JFrame section out, and then changed the graphAttempt to look like this:
    graphAttempt grph = new graphAttempt();
    //grph.getSlope();
    //grph.getIntercept();
    grph.calculateY();
    grph.setSize(450,450);
    grph.setLocation(200,200);
    grph.setVisible(true);
    The above code seemed to run fine, but didn't display anything. It did however only run through once. But it still showed that the variables were reset to their default value.

    I believe I need to have a JFrame in there but I'm not sure.

  18. #38
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Do you understand that you are creating two objects, putting data in one and showing the other one?
    Why do you create two objects?
    graphAttempt grph = new graphAttempt(); // creates the first one that gets the data
    f.add(new graphAttempt()); // creates the second one without the data and shows it in the frame

  19. #39
    only1zster is offline Member
    Join Date
    Dec 2011
    Posts
    24
    Rep Power
    0

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    I do now.
    Now I'm not sure how to do it all in one. I've tried several different ways (as noted in #37). I'm not sure where to go now. This is harder than I thought it was going to be.

  20. #40
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: How do you graph a simple line with user inputed slope and intercept?

    Only create ONE instance of the class and use its reference in place of creating a new instance.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. How do i delete a section of a user inputed string that i input
    By DevenGosalia514 in forum New To Java
    Replies: 8
    Last Post: 03-18-2011, 04:02 AM
  2. Replies: 5
    Last Post: 10-01-2010, 09:26 PM
  3. help in line graph in jsp page
    By javastuden in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-15-2010, 04:20 AM
  4. Plotting a line graph
    By revilo in forum New To Java
    Replies: 1
    Last Post: 10-11-2009, 12:07 AM
  5. Averages of user inputed values (Need Help)
    By Zebra in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •