[java] Graphics2D Overlay

Started by
5 comments, last by dmatter 15 years, 11 months ago
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!
Advertisement
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.
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
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.
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.
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.
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.

This topic is closed to new replies.

Advertisement