-4

I'm writing code that moves a token piece around a monopoly board based on certain coordinates. Currently, it prints it in each square but I'm trying to get it to print in each square, with a timer so you can see each square printing and when it prints in one square, it removes the print in the previous square so the token can only be in one square at a time. This is the code I have so far:

for(int g=0;g<10;g++)
         {
             JLabel redtoken = new JLabel(new ImageIcon ("src/TokenRed.png"));
             redtoken.setBounds(x[g],y[g], 10, 10); // Size and position set
             LPane.add(redtoken, new Integer(3)); // Red token set as layer 3


         } 

Test using a selection of coordinates: (Token is Red Square)

enter image description here

Gavin Coll
  • 89
  • 1
  • 1
  • 11

1 Answers1

1

If I understand correctly, you want to see a red token hopping from square to square. I suggest something like that :

Object lastPrinted = null;
for(int g=0;g<10;g++)
{
    if(alreadyPrinted != null){
        deleteToken(alreadyPrinted);
    }
    printNewToken(g)
    try {
        Thread.sleep(300);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        System.err.println(e);
    }

}
Jeremy Grand
  • 2,300
  • 12
  • 18
  • Thanks for this but I'm getting an error `Unhandled exception type InterruptedException` on `Thread.sleep(300);` – Gavin Coll Feb 17 '17 at 16:43
  • The Programme currently takes a few seconds to load and then the token appears on the last square in the array when it opens – Gavin Coll Feb 17 '17 at 16:45
  • That means you only print the token after the execution of the method. You then need to loop over the printing method. – Jeremy Grand Feb 17 '17 at 16:47
  • Ah I see. How would I go about this? I'm new to Java so not really sure how to do this – Gavin Coll Feb 17 '17 at 16:51
  • the above code doesn't seem to be working for `alreadyPrinted` and `printNewToken` – Gavin Coll Feb 17 '17 at 17:07
  • Yes, because the method deleteToken and printNewToken do not exist. As I don't have access to the rest of your code I can't possibly be aware of what is in charge of printing the red token on your board. I just wrote this code as an example to how you should write your loop rather than a ready-to-use solution. I'm sorry but I can't do everything for you. Thus, you have to understand what in your code is printing the red token and iterate over that operation. – Jeremy Grand Feb 20 '17 at 15:20