title: Java Swing Timers & Threads &only last command is being executed in For Loop
body: <p>Hope you are all well.</p>
  
  <p>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.</p>
  
  <p>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.  </p>
  
  <p>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.  </p>
  
  <p>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. </p>
  
  <p>Here is the movement class which moves the vehicle forward 1 meter at a time using a Swing Timer.<br>
  It implements Runnable.  It's abstract because I needed the run method to take the user input commands as a parameter.  Is that what's causing the problem?</p>
  
  <pre><code>public abstract class Movement implements Runnable {
  
  
      static int count = 0;
      static int distance;
  
  
      public static void moveRover() {
          // Every 1 second the timer will move the Rover 40 pixels.
          at.translate(0, 40);
      }
  
      public static void getDistance(int num) {
          distance = num;
  
      }
  
      static Timer timer = new Timer(500, new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
  
              moveRover();
              count++;
              System.out.println(count);
  
              if (count == distance) {
  
                  stop();
                  System.out.println("Timer stopped");
                  count = 0;
              }
          }
      });
  
      public static void start(int num) {
          getDistance(num);
          System.out.println("Distance rover will travel: " + distance);
          System.out.println("\nTimer started");
          timer.start();
      }
  
      public static void stop() {
          timer.stop();
      }
  
      public void run(int num) {
          start(num);
      }
  
      public Movement(int num) {
          System.out.println("Start RUN method");
          run(num);
  
      }
  </code></pre>
  
  <p>And here is the code from another class which runs the for loop and thread:</p>
  
  <pre><code>public static void action(final String[] commands) {
  
          for (int i = 0; i &lt; commands.length; i++) {
  
              if (isInteger(commands[i])) {
                  int distance = Integer.parseInt(commands[i]);
  
                  Movement h1 = new Movement(distance) {
                      @Override
                      public void run() {
                          System.out.println("Empty run method");
                      }
                  };
  
                  Thread t1 = new Thread(h1);
  
                  t1.start();
                  System.out.println(t1.isAlive());
  
                  try {
                      t1.join();
                  } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
  
              } else {
                  Direction.start(commands[i]);
                  String pos = Direction.getDirection(); // GET DIRECTION
              }
          }
      }
  </code></pre>
  
  <p>I really appreciate you reading all that, thank you so much!</p>
  
  <p>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 :D</p>