Which containers / graphics components to use in a simple Java Swing game?

Posted by rize on Stack Overflow See other posts from Stack Overflow or by rize
Published on 2010-04-03T16:34:13Z Indexed on 2010/04/03 16:43 UTC
Read the original article Hit count: 282

Filed under:
|
|
|
|

I'm creating a simple labyrinth game with Java + Swing. The game draws a randomized labyrinth on the screen, places a figure in the middle, and the player is then supposed to find the way out by moving the figure with arrow-keys. As for now, I'm using a plain background and drawing the walls of the labyrinth with Graphics.drawLine(). I have a custom picture of the figure in a .gif file, which I load as a BufferedImage object.

However, I want the player to see only part of the labyrinth at a time, and the screen should follow the figure in the game, as the player moves around. I'm planning to do this by creating an Image object of the whole labyrinth when it is created, and then "cutting" a square around the current position of the figure and displaying this with Graphics.drawImage(). I'm new with Swing though, and I can't figure out how to draw the figure at different positions "above" the labyrinth without redrawing the whole thing. Which container/component should I use for the labyrinth and then for the figure to achieve this?

© Stack Overflow or respective owner

Related posts about java

Related posts about swing