If the user is not entering numbers in the textfields, initialize them with numbers:JTextField one = new JTextField("25", 3);
Is that because their values are all being changed by the same amount: 1at the same speed.
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.
If the user is not entering numbers in the textfields, initialize them with numbers:JTextField one = new JTextField("25", 3);
Is that because their values are all being changed by the same amount: 1at the same speed.
If you don't understand my answer, don't ignore it, ask a question.
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
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().Integer.parseInt isnt working
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.
this is the new error im getting:
after adding this to get my x's and Timer to work right: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)
//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(); } }
Why is the code passing a null value to the parseInt() method called on line 72?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)
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.
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.
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.
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.
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.
would the:
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?String text1 = one.getText(); int Car1 = Integer.parseInt(text1);
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.
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(); } }
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.
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.
Where do each of the cars get their speed from? Check the code.they all go the same speed as Car1.
If you don't understand my answer, don't ignore it, ask a question.
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