简体   繁体   中英

Java swing keylistener

I have simplified my code and i was able to successfully create key bindings. So now in my maze i have an element(red square) that i want to move right when pressing the right key. I have the class Test and the class Element. The element has a position defined by the attributes x and y. The function printFrame displays the maze in a JPanel with a Gridlayout. The element position is a red square. When the right key is pressed the element position is updated by the RIGHTaction class. Now how can i update the mazePanel with the element new position? Should i update the cells array with the new element position and the JPanel is updated automatically? And what about the previous position of the element? Should i set the gray color for that position so i dont get two red squares? If i want the code to stop when the element position is x==9 and y==7 how should i do it? Shoul i create a loop for the program to run while the element position is different from the final position? Where should i put that loop? My new code is in the links: Test class: http://pastebin.com/d0n7qwqa Element class: http://pastebin.com/HBWXt6zd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM