Hi everyone,
Hope you are all well.
Also I'm so sorry for how long winded this is. And I'm new to
Java so please forgive me for my lack of knowledge/terminology/Java conventions.
Basically I've created a program which takes user input and moves the vehicle across the surface area. So the user input can be "50 left 4" so that means go 50 meters forward, turn left and go 4 meters forward. For the vehicle I'm using a paint method on a JPanel. When the vehicle moves forward, it initially jumped from one side of the area to another. I wanted to be able to see it moving meter by meter. So I added a Swing Timer which moves the vehicle 1 meter, pauses for a second, moves the vehicle 2 meter and so on.
Now, the problem is that when I enter the commands "50 left 4", the vehicle simply turns left and then moves 4 meters forward. It ignores the first number command. The same happens when I enter "3 4", it will ignore 3 and just move 4 meters forward. Also when I enter "3 left", it will turn left first and then move 3 meters forward. Now I've got methods which takes the user input, chop it up into an array, feed each element of the array through a loop, check if it's an integer or not. If it is an integer it moves the vehicle forward, if not it turns the vehicle either left or right. That all works fine and I'm happy with it.
So what I thought I'd do is have the class which moves the vehicle implement Runnable so that this method will be executed from a separate
thread, making the main thread wait and that way it will won't ignore every command except for the last. But that doesn't work either.
Here is the movement class which moves the vehicle forward 1 meter at a time using a Swing Timer.
It implements Runnable.
And here is the code from another class which runs the for loop and thread:
I really appreciate you reading all that, thank you so much!
And thank you to anyone who replies! If you know where I'm going wrong and what I can do to fix it, you're an absolute lifesaver, I really appreciate it