Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    How were you able to write such a large, complicated program and then give up?

    You need to know how to debug the code. One way is by adding println() statements that show you where the execution goes and what values variables have at each point.
    You MUST have some ideas about where the execution should go and how variables change as you go thru the code.
    By printing out the trace you should be able to see where the program doesn't do what you want it to do. Then you need to look at that code and see why it does what it does.

  2. #22
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    How were you able to write such a large, complicated program and then give up?

    You need to know how to debug the code. One way is by adding println() statements that show you where the execution goes and what values variables have at each point.
    You MUST have some ideas about where the execution should go and how variables change as you go thru the code.
    By printing out the trace you should be able to see where the program doesn't do what you want it to do. Then you need to look at that code and see why it does what it does.
    The username should work fine. But I need to redraw the gamePanel because I can't call the loginPaint (in the drawLogin class) method in the keyTyped method?.. or can I?
    Because the loginPaint has the Graphics g in it.
    Last edited by PhQ; 07-19-2010 at 06:50 PM.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    should work fine
    Famous last words.

    Your code is too big for anyone to easily explain to you how to pass references from one method to another.
    In one method you are collecting data. When you get all the data you need for the paint() method to show, then call repaint() and have the system call your paint method which should have the data that was collected and be able to draw it.

  4. #24
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    Famous last words.

    Your code is too big for anyone to easily explain to you how to pass references from one method to another.
    In one method you are collecting data. When you get all the data you need for the paint() method to show, then call repaint() and have the system call your paint method which should have the data that was collected and be able to draw it.
    Ok, for some reason, the username is nothing in the drawPaint method.
    I am trying to do this:
    Java Code:
    		g.drawString("" + app.username2, 273, 221);
    and the app is my runApplication class (where all the data is collected)
    and variable should get changed here.
    Java Code:
    sb.append(ke.getKeyChar());
    		gamePanel.repaint();
    		sM("Username in runApp is " + sb.toString());
    		username2 = sb.toString();
    		sM("Username2 in runApp is " + username2);
    		gamePanel.repaint();
    What should I do?
    Last edited by PhQ; 07-20-2010 at 01:25 PM.

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    Trace thru the program to see when username is set to a value and when you try to draw it on the screen. You have to set it before you can draw it.
    One way to Do the tracing by putting println()s at where its set and where its used.

  6. #26
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    sM() <- is same as println()

    Java Code:
    public void sM(String s){
    		System.out.println(s);
    	}
    and when I type a letter I get

    Username in runApp is a
    Username2 in runApp is a
    Loading game panel..
    Username2 in drawLogin
    drawLogin has been called.
    Username2 in drawLogin

    should be

    Username2 in drawLogin a

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    For a bit more clarity in the printout of the contents of a string, surround it with delimiters. Something like:
    println(".... >" + theString + "<");

    So where is username being cleared?
    In runApp it is "a"
    In drawLogin its "" (empty)

    A problem could be that there is more than one copy of an object floating around. You set the value of username in one and then call the other one to draw it. The other one still has the empty value.
    Put a println() in the constructor for the class holding username to see if you have more than one.
    To see who is creating the extra copy add the following:
    try{throw new Exception("Who called?");}catch(Exception x){x.printStackTrace();};

    You'll get a stack trace for every call.

  8. #28
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    For a bit more clarity in the printout of the contents of a string, surround it with delimiters. Something like:
    println(".... >" + theString + "<");

    So where is username being cleared?
    In runApp it is "a"
    In drawLogin its "" (empty)

    A problem could be that there is more than one copy of an object floating around. You set the value of username in one and then call the other one to draw it. The other one still has the empty value.
    Put a println() in the constructor for the class holding username to see if you have more than one.
    To see who is creating the extra copy add the following:
    try{throw new Exception("Who called?");}catch(Exception x){x.printStackTrace();};

    You'll get a stack trace for every call.
    Ok I will do that, I just have a question: Is it possible to add a JTextField to the paint?

    Ok, it works perfectly if I do g.drawString(username2, 273, 221); in my runApplication class (where all the data is collected)
    Last edited by PhQ; 07-20-2010 at 02:32 PM.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    Is it possible to add a JTextField to the paint
    That makes no sense. Apples and oranges.
    A JTextField is a component that is added to a container. It has a lot of logic behind it.
    The paint method uses a Graphics object to draw on a graphics context.

  10. #30
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    That makes no sense. Apples and oranges.
    A JTextField is a component that is added to a container. It has a lot of logic behind it.
    The paint method uses a Graphics object to draw on a graphics context.
    Ok, it works perfectly if I do g.drawString(username2, 273, 221); in my runApplication class (where all the data is collected)

    and I had a look everywhere, the username2 is changed once, and it is in the place where I want it to change.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. foreign keys in jtable gui
    By blackpearlmoni in forum New To Java
    Replies: 0
    Last Post: 10-31-2009, 05:52 PM
  2. Replies: 0
    Last Post: 05-13-2009, 04:55 PM
  3. auto generated keys
    By abhi_iips in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 08:14 AM
  4. Media Keys
    By tikigod in forum New To Java
    Replies: 0
    Last Post: 01-29-2009, 12:30 AM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 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
  •