Domanda

I didn't attach any code because I didn't think it was necessary if you just follow along.

I have a main class with a JComponent (component) and JFrame (frame) as fields. This class also implements runnable and once the thread is started, a game loop runs and currently the only line in that loop is component.repaint(); usually paintComponent() is constantly called and my window continues to update smoothly. I also added a keylistener to the JFrame using frame.addKeyListener(new MoveListener()); (I made MoveListener and it implements KeyListener)

The only problem is that when I hit say the "right" key and key pressed is called. Instead of having my sprite jump from one tile to the next im having him smoothly move one pixel at a time very quickly(my animation). To keep the sprite centered on the screen, when he "moves right", then I have the map move left. In key pressed, I have a for loop that subtracts 1 from the coordinates of the map each time until it reached the next tile in which case it would stop moving and everything's coordinates would be updated to the next tile.

I figured the keylistener and my thread would run independently, but when keypressed is called... the thread keeps running, but the component isn't repainting. The screen pauses for a moment as the for loop runs through and then the sprite jumps to the next tile. I've tested and im sure the thread is running and attempting to call repaint() but paintComponent() doesn't run untill after keypressed is finished.

I assumed that since they were separate threads that the component could keep painting as the listener moved the map around. Is there a reason it won't paint while the keylistener is running or did I do something else wrong because obviiously I'm not a profesional so I don't know everything about this stuff? also what is the best way to get around this...if need be Ill upload my code so you can take a look, but i think i explained pretty thoroughly. thanks for all help in advance!!

È stato utile?

Soluzione

I have a for loop that subtracts 1 from the coordinates of the map

Would suggest that you blocking the event dispatching thread, which is responsible for processing paint requests AND key event (amongst other things)

This means that while the loo in keyPressed is been executed, nothing else will be processed, including the repaint request.

Before you comment that you are painting the component from a separate thread, you're not, actually. What happens is you make a paint request to the RepaintManager, which places a repaint request into the event queue, which is then processed by the Event Dispatching Thread

What I suggest you do, is raise some kind of flag in keyPressed event method. You would, then monitor for this flag in the game thread and update the state of the map on each iteration of the thread loop until the end state is meet and/or some kind of event changes the state of the flag, for example...

You might like to take. Look at Painting in AWT and Swing and Concurrency in Swing for more details

And while I'm here, I would encourage you to use key bindings over KeyListener as it has better control over the focus requirements

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top