In C# I saw already the SpriteBatch class (In the XNA plugin for XBOX games) . In that class is it posible to paint in layers. Now I want to do the same in Java because I'm making braid (See my other questions) and I have an ArrayList from all the GameObjects and that list is not made in the correct paintoreder.
For exemple: I have a door but the door is painted on the player.
Martijn
Sorting the list of items should solve your issue, but if you do find you need to paint layers, you can do it like this:
Create a BufferedImage for each layer
BufferedImage[] bi = new BufferedImage[3];
bi[0] = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Paint into the buffered images
Graphics2D bg2 = bi[0].createGraphics();
bg2.drawXXX(...);
That should all be outside the actual paint method.
In the paint or paintComponent method, use alpha compositing to assemble the layers
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
g2.setComposite(ac);
for (int i = 0; i < bi.length; i++) {
g2.drawImage(b[i], 0, 0, this);
}
How about sorting the items before rendering? The background items have to be painted first and the foreground ones last.
If you have a List and items are Comparable you can use
Collections.sort(list);
If you need a special order, you can implement your own Comparator.
All that of course requires the items to hold some info on their z-position.
And you shouldn't do this in the paint method. sort items when they're changed, ie. added.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.