Question: I'm making an application and I require some help please.

In flash, gameLoops can be either made with an enterFrame, or timer.

I am new to Java so please forgive me arrogance. From researching I've seen people using timers.

Now I Have a tile based game, where if the user clicks on a grid (It's 30\*21) the player will go to that grid. 

Here is what I've come up with

```
    for(int i = 0; i<30; i++)
    {
            for(int j = 0; j<21; j++)
            {
                if(e.getSource()==JbGrid[i][j])
                {
                   MOUSE_X = j;
                   MOUSE_Y = i;
                   System.out.println("Mouse X: "+MOUSE_X+" Mouse Y: "+MOUSE_Y);
                   moveToSquare();

                }

            }
    }

}

public void moveToSquare()
{

    if( BPOS_X < MOUSE_X )
    {
        BPOS_X +=2;
    }

    if( BPOS_X > MOUSE_X )
    {
        BPOS_X -=2;
    }

}

```

Now the problem is that the game doesn't physically show the red block moving towards the MOUSE\_X position, this is because the page isn't being refreshed.

Is there a way I can implement a gameLoop in to my code. It's just that most people implement it at the start, and I never knew about "gameLoops" before I started my project.

Can someone please point my in the right direction

```
if(player clicks square)
activate loop
move player to Mouse_X

```

Thank you.

![You can see that it doesn't leave a trail of red blocks behind](https://i.stack.imgur.com/UTSY1.png)

Answer: 
It looks like you're using Swing, and if your question is how to get a game loop out of an application using the basics of Swing (an application not using BufferStrategy, but a combination of different Swing components), then you roughly want the following:

* An object that represent the games state (can be further abstracted down into more objects of course)
* A custom JComponent that has a reference to that object, which draws the object in someway during a repaint (can be further abstracted down into more JComponents that help draw the state, custom or not)
* A java.util.Timer that also has a reference to that object, which updates the game based on how much time has elapsed since the last update, and that eventually calls repaint

The custom JComponent you create can have listeners on various buttons or other things such as mouse clicks, where you can use the same reference to the game state object you use to draw to update the game's state based on the UI actions.

Make sure your code is thread safe too as the repaint executing on the EDT, or one of your Swing listeners, can occur at the same time you update your state with the Timer object.

If you search online for example usage of game loops in Java using BufferStrategy you'll find more traditional game loop examples.
<http://www.java-gaming.org/index.php?topic=24220.0>

read this and ask something more, if you don't understand anything