Jump to content
  • Advertisement
Sign in to follow this  
Fragsta

[java] Graphics2D Overlay

This topic is 3715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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.

Share this post


Link to post
Share on other sites
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!