Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 41 of 41

Thread: Help with racecar applet?

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    If the user is not entering numbers in the textfields, initialize them with numbers:
    JTextField one = new JTextField("25", 3);

    at the same speed.
    Is that because their values are all being changed by the same amount: 1
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    Is there a way i could use my textfield text(for example: 25 is entered) to change the speeds for my pictures instead of making them all go the same speed? also a way to change the text into an int, because Integer.parseInt isnt working

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Integer.parseInt isnt working
    Please explain what happens. Add some println() statements to the code that prints out the value of the String passed to the parseInt() method and to print out the value of the int returned by parseInt().

    If you want the positions of each picture to change by different amounts, add a different amount to their x,y location values.
    x++; // adds one to x
    x = x + 4; // adds four to x
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    this is the new error im getting:
    java.lang.NumberFormatException: null
            at java.lang.Integer.parseInt(Integer.java:417)
            at java.lang.Integer.parseInt(Integer.java:499)
            at superclass.init(superclass.java:72)
            at sun.applet.AppletPanel.run(AppletPanel.java:424)
            at java.lang.Thread.run(Thread.java:619)
    after adding this to get my x's and Timer to work right:
    //in init:
    one = new JTextField("",3);
    text1 = one.getText();
    Car1 = Integer.parseInt(text2);
     
    two = new JTextField("", 3);
    text2 = one.getText();
    Car2 = Integer.parseInt(text2);
     
    three = new JTextField("", 3);
    text3 = one.getText();
    Car3 = Integer.parseInt(text3);
     
    four = new JTextField("", 3);
    text4 = one.getText();
    Car4 = Integer.parseInt(text4);
     
    //what i have in ActionListener for timer:
     private class TimerHandler implements ActionListener
    {
     
    	public void actionPerformed(ActionEvent event)
     
    {
     
    	a++;
    	a=(Car1 + (a));
    	b++;
    	b=(Car2 + (b));
    	c++;
    	c=(Car3 + (c));
    	d++;
    	d=(Car4 + (d));
    	repaint();
     
     
     
    }
     
     
    }

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:417)
    at java.lang.Integer.parseInt(Integer.java:499)
    at superclass.init(superclass.java:72)
    Why is the code passing a null value to the parseInt() method called on line 72?

    one = new JTextField("",3);
    text1 = one.getText();
    Car1 = Integer.parseInt(text2);  <<<<<<< note text2 here NOT text1
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    that's what im wondering. the line 72 in my code is:
    [CODE]
    Car1 = Integer.parseInt(text1);

    [\CODE]
    it seems to not want to do anything with turning the text into an int.

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Is the value of text1 null? How and where does text1 get assigned a value? How does it get a null value?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    i was trying to set the value of text1 up with the text of the JTextField, and was gonna use the text in an Integer.parseInt for the Car1. i never declared text1 as null or any of that.

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Where does text1 get assigned a value? It will be null if no value is assigned to it. If it is null, then parseInt() will throw the exception. So you must change the code to make sure text1 gets a value.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    would the:
    	String text1 = one.getText();
    	int Car1 = Integer.parseInt(text1);
    be assigning it a value? i was giving it the text of the JTextField. the problem i guess is that until the app is initialized, then no value can be assigned because the user hasn't put them in yet. is there a way i could make it to where the code would have to wait until AFTER i initialized, entered number into the textbox, and clicked the button to get it to read it right?

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    If you are getting the numbers BEFORE the user has entered them, that doesn't make sense.
    The program should show the GUI and wait for the user to enter the data and to tell the program that the data is ready and that it can now get the data from the input fields where the user entered it. One way for the user to tell the program that the data is ready is by having a button the user presses when the data is ready.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    i moved the code that i had up there to the TimerHandler:
    //this is the code for my timer in the init:
    timer1 = new Timer(50, new TimerHandler());
     
    //this code is for my timer to work
    private class TimerHandler implements ActionListener
    {
     
    	public void actionPerformed(ActionEvent event)
     
    {
    	text1 = one.getText();
    	Car1 = Integer.parseInt(text1);
     
    	text2 = one.getText();
    	Car2 = Integer.parseInt(text2);
     
    	text3 = one.getText();
    	Car3 = Integer.parseInt(text3);
     
    	text4 = one.getText();
    	Car4 = Integer.parseInt(text4);
     
     
     
    	a++;
    	a=(Car1 + (a));
    	b++;
    	b=(Car2 + (b));
    	c++;
    	c=(Car3 + (c));
    	d++;
    	d=(Car4 + (d));
    	repaint();
     
     
     
    }
     
     
    }

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    How does the user tell the program that there is data now? The timer is going to get the data every 50ms even if the user is not ready.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    the timer doesn't start until the button is clicked, right? and yes i dont get the null error or any other exception now, the only problems im having is:
    a) trying to get the images to start back at the beginning when the stop button is clicked and the the start button is clicked again, and
    b) trying to separate the image from traveling together because no matter the speed i put in for each text field, they all go the same speed as Car1.

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    they all go the same speed as Car1.
    Where do each of the cars get their speed from? Check the code.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    i did, and i got it working now. but do you know how to add music files to a java applet? i have the code and everything working now, thanks to your assistance and some critical thinking, but i need to do both a music file and want to try out a double buffer

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Applet help
    By Javan00bz in forum Paid Java Projects
    Replies: 4
    Last Post: August 20th, 2013, 02:23 PM
  2. Applet help
    By Javan00bz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 13th, 2013, 07:46 AM
  3. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM
  4. Applet
    By needhelp101 in forum AWT / Java Swing
    Replies: 16
    Last Post: November 7th, 2011, 06:22 PM
  5. Replies: 0
    Last Post: October 13th, 2011, 07:42 PM