Sign in to follow this  

[java] z-layers possible with java.awt.canvas ?

This topic is 4588 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

Looking through the documentation for the Canvas class, I didn't see any methods that would allow anything to be placed at a specific z-level. From what I can tell so far, it seems that for every time images are placed so that they overlap, the one placed earlier will be at a lower z-layer than the more recent call. Is there a way to explicitly declare what layer an image would be placed on? I ask this because of an idea I'm considering for a game. For example, taking a tilemap, if all non-transparent "ground tiles" are laid first, then non-transparent "above-ground tiles" (objects to walk behind/under, etc.) are laid next, and the transparent "character tile" is laid last, how would I be able to ensure that the character doesn't do something like go under the ground or in front of something that's supposed to cover them? Since I can't find explicit z-ordering in java at the moment, is there another way short of Java3D? I'll be testing this out myself in a few days, as soon as I create a few more .png images to play around with, but I'm also interested in knowing this beforehand.

Share this post


Link to post
Share on other sites

Hi!

I’m not sure if I’ve got your problem, but it seems that your looking for something like JLayeredPane that you can find in Swing…
But in fact, if you’re working with a 2D tile based game, there’s no need for you to use such a thing, since you can just draw the images in the order that its logical flows…



Share this post


Link to post
Share on other sites
I didn't think about using JLayeredPane (probably with a Canvas for each layer) in Swing, but that could work. I'll try that eventually if my next idea doesn't work. At this point, I guess I'll assume that for any two or more images that overlap, the one called earlier will be obscured (at least in part) by the one called later.

Share this post


Link to post
Share on other sites
A Canvas doesn't provide any management of the graphics you render to it. It defines the bounds of an area of the screen to which you can draw, and beyond that doesn't do much else. If you want z-buffer management, you will have to implement it yourself.

Share this post


Link to post
Share on other sites

This topic is 4588 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.

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