2

I was hoping to stack instances of JLayers ontop of each other to get a layered effect but they don't seem to want to work that way. I'd like to decorate a single JPanel with multiple effects implemented by multiple LayerUI<JPanel> classes.

For example

frame.add(new JLayer<>(panel, new LayerUI<> ...));
frame.add(new JLayer<>(panel, new LayerUI<> ...));

where the argument to new JLayer is the same JPanel instance but the LayerUI instances do different things.

It seems the last one added wins. They both draw directly to the context in the paint method, perhaps thats it..?

See here for Oracles' tutorial

Toby
  • 9,523
  • 8
  • 36
  • 59
  • This cannot be done for a different reason: same UI element (panel in this case) cannot be used more then once in the same UI hierarchy – Eugene Ryzhikov Aug 01 '13 at 18:57
  • for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable – mKorbel Aug 01 '13 at 18:58
  • It's been a few years since this was posted, but I'd interested in an answer to this. I've been searching around for a clean way to add multiple decorations to a JPanel/JComponent with no luck for hours now. – Forrest May 14 '17 at 09:51

1 Answers1

0

I'm not familiar with JLayer since it's new with Java 1.7.

However, you can write your own Layer class that contains a BufferedImage and a z level.

Your draw method would go through a List or Map of Layers, drawing them on a JPanel in z level order.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111