Sign in to follow this  
Fragsta

[java] Graphics2D Overlay

Recommended Posts

Fragsta    115
I'm making this really simple maze game as part of a little project to understand how to use Graphics2D in Java, and so far it's been going well the way I've been doing it. Basically, there's a maze, and a player, all represented by differently-coloured 2-dimensional blocks. This is all drawn onto a MazePanel (which is a class extending JPanel). In MazePanel is a paintComponent() method. When a MazePanel is initiated in a JFrame, a KeyListener listens for the player's command to move their block through the maze. Every time a move is made, the position of the player in the MazePanel is simply edited, and it is completely redrawn. This doesn't seem like the most efficient way of doing things, and I'd rather have the player be separate from the MazePanel, so I need to make some sort of overlay I think, but I don't know how and searching Google hasn't really helped with it so far. Here's a diagram to help better illustrate my problem: http://www.silverferret.co.uk/hosted/mazepanel.jpg I hope someone can help. Thanks!

Share this post


Link to post
Share on other sites
I'm not sure how experienced you are with graphics in general, but what you are doing is correct. All the entities are drawn onto one pane, whether its jframe or jpanel (I prefer drawing on jframe when using graphics 2d because of double buffering), and redrawn every game tick.

Share this post


Link to post
Share on other sites
greggles    347
It sounds like the maze stays constant, but the player moves around, correct? In that case, you could probably draw the maze onto an image (if you don't know what type of Image to use, than you could probably start with a BufferedImage), then draw the image underneath the player every time you paint.

Greggles

Share this post


Link to post
Share on other sites
Fragsta    115
Right, thanks very much :) I'll just keep on as I'm doing now. Might switch in JOGL soon though, once I feel I've gotten the hang on Java2D a bit more.

Share this post


Link to post
Share on other sites
RobAU78    206
I wouldn't worry too much about the fact that everything is redrawn whenever the player moves. The alternative would be to "erase" where the player was, redraw that part of the maze, and then draw the player where it is now. For a simple maze game like this, doing that kind of thing isn't really necessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fragsta
Right, thanks very much :) I'll just keep on as I'm doing now. Might switch in JOGL soon though, once I feel I've gotten the hang on Java2D a bit more.


If you do decide to go down the opengl route, i suggest LWJGL over JOGL.

Share this post


Link to post
Share on other sites
dmatter    4821
Quote:
Original post by Karan Bhangui
If you do decide to go down the opengl route, i suggest LWJGL over JOGL.

Seconded. [smile]

Though, just to be clear, comparing the two is a little unfair: JOGL is for graphics only; LWJGL combines graphics, audio and input.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this