0

I have this timer used to move Star.png through JFrame & JPanel

function to move the star which has the timer :

private final static int HEIGHT = 300;
.
.//more code here
.
.
  x=y=0;
.
. 

public void downRight() {
    Timer localTimer = new Timer(100, null);
      localTimer.setRepeats(false);
    localTimer.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {    
                x++;
                y++;
                repaint();
        }
    });
    int xTest=0;
    while (y < HEIGHT) {
        System.out.println("x "+(++xTest)+" y "+y);
        localTimer.start();
   }
    System.out.println("Reached");
}

When Running the timer and testing xTest,y values found the following:

x 1 y 0

x 2 y 0

x 3 y 0

..... More Outputs here

.....

x 1653 y 1

x 1654 y 1

......

......

x 285836 y 299

x 285837 y 299

Reached

So What is happening here? Why xTest is too greater than y although the both are in the same scope ?

Youans
  • 4,801
  • 1
  • 31
  • 57
  • 1
    Consider creating and posting an [sscce](http://sscce.org). I also don't like the smell of your `while (y < height)...` block that appears to be repeatedly called in the EDT, the Swing event thread. This will repeatedly call `start()` on the Swing Timer. And what's to prevent xTest from getting very large? All in all, a very confusing question I have to say. – Hovercraft Full Of Eels Mar 02 '13 at 21:28
  • 1
    @HovercraftFullOfEels That loop is not called on the EDT (if it was called on the EDT, `y`'s value would never increase). – Jeffrey Mar 02 '13 at 21:30
  • @Jeffrey: I suppose you're right, but then what thread is it called on? Again, I think that this question is far from clear and could stand quite a bit of cleaning up and exposition of detail. – Hovercraft Full Of Eels Mar 02 '13 at 21:32
  • Guys I'm new in Swing and I have fare Info about Threads , All I had to do is to Control the timer when to run the actionPerformed(ActionEvent e) method and where not to – Youans Mar 02 '13 at 21:37
  • Your code needs to be cleaned up a lot, and you need to address our concerns as noted above. Again, why are you call `start()` on the Timer multiple times? Where are you calling `stop()` on the Timer? Where is that while loop being run? Why not post an [sscce](http://sscce.org)? I suspect that you want to start the Timer when the right arrow is pressed, and stop it when it is released. If so, then create a small program that demonstrates just this. You will want to use Key Bindings to capture your key presses and releases, and will likely want to elevate the Timer out of the method. – Hovercraft Full Of Eels Mar 02 '13 at 21:39
  • I know that my code is so bad but the problem I want to solve is conditioning the actionPerformed function so on y==300 pause the timer since I can't reach the timer from actionPerformed Function I thought that may solve the problem ,But now it's obvious that I know nothing about Swing Timer,Even I don't know SSCCE or EDT are – Youans Mar 02 '13 at 21:45
  • @YouYou: I have given you several *links* to a reference on [sscce](http://sscce.org). Perhaps you should click on the link before saying that you don't know what it is? Also, you can find an example of using a Swing Timer with key binding and moving an image to be found [here](http://stackoverflow.com/a/6887354/522444). Also, if you are going to be doing any Swing coding, you'll need to learn about the event dispatch thread. Reference to be found [here](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) – Hovercraft Full Of Eels Mar 02 '13 at 21:47
  • @Hovercraft Full Of Eels: I did actually clicked on the links and I'm reading them now.I was just answering your questions about why not to use SSCCE ,Thanks :) – Youans Mar 02 '13 at 21:51

1 Answers1

5

The reason xTest and y do not have the same value is because a Timer has an initial delay (which is set to the delay provided in the constructor). It will take 100 ms after you call start in order for y's value to increase by 1. In the meantime, xTest is allowed to increase as fast as it can.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • So it's now clear that the while block is running on a thread differs from the timer's one and all are working together at the same time ,Right? – Youans Mar 02 '13 at 21:48
  • 1
    @YouYou Your while loop is running on one thread, and the `Timer` will fire its events on the [Event Dispatch Thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). Both of these threads are running at the same time. – Jeffrey Mar 02 '13 at 21:49