Random
Random

Reputation: 1

Updating location of JPanel

im currently working on a game in Java and a problem occured. It is a strategy game.

My game area or map is a JPanel, which i put in a JScrollPane, allowing you to scroll around the map. I put other JPanels on the map, for example a bar for info about your current resources. Of course when i scroll, theese JPanels doesnt follow, and now i wonder whats my best tactic for this?

For now, im using the keyboard arrows to scroll, i then use setBounds of the JPanels i wanna move in the same direction. For example , for the right arrow key i increase the value of the x value in the setBounds parameter (x, y, sizeX , sizeY). This isnt very pretty and if u hold the right arrow key for some seconds, the JPanel which has an image, starts flickering a bit.

What are my options? Thanks for your time!

Upvotes: 0

Views: 286

Answers (1)

aioobe
aioobe

Reputation: 420921

I would put the "resources"-bars etc outside of the scroll panel. I.e., let the main panel have a BorderLayout and let the map-area be the center component, and put the resources-bar as south component.

      Main panel (BorderLayout)
 _______________________________________
|  ___________________________________  |
| |                                 |A| |
| |                                 | | |
| |                                 | | |
| |     Center component:           | | |
| |                                 | | |
| | (Scrollpane containing the map) | | |
| |                                 | | |
| |                                 | | |
| |_________________________________|v| |
| |<_______________________________>|   |
|                                       |
|  ___________________________________  |
| |                                   | |
| | South component: Resources        | |
| |___________________________________| |
|_______________________________________|

Upvotes: 1

Related Questions