• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I layer components on top of each other without blocking lower components?

 
Jason Robey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use a JLayeredPane containing a root JPanel with an image as a background and several toggleable layers with JPanels as the same size as the icon in the root JPanel. When I make any of the higher levels visible, the JPanel covers the entire image in the layer below. Is their a way to make this transparent--i.e., not draw the grey background? Maybe there is a better way to do layered image manipulation?
Jason Robey
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
panel.setOpaque(false);
 
Jason Robey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I tried that and it still covered the layer underneath with grey. I switched to using JLabels now instead of JPanel, but still the same effect. Upper layers conceal lower layers.
Jason
 
Jason Robey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out that with a layout manager (as is documented in the API) layering information is ignored. So even if my JPanel is set to transparent, the layout managers I tried only look at the top (this is because all my frames are of the same size in different layers). So, I got it to work making each layer a JLabel and using setBounds to give them all the same size. I used setIcon on each JLabel to an implementation of icon specific to the function of each layer. My only issue now is efficiency. For the small set of things I am dealing with, what I have works. The painting of the specialty icons seems to slow my application down more than necessary, but only when I have an area with several objects in the view space of my scroll pane. If anyone has any ideas on how to make this more efficient, I'm all ears. Thanks.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not make a custom Icon subclass that can turn on and off "overlay" icons on top of a default background icon... I don't think that multiple overlapping components are a good idea, as you get overhead from the extra components, but all you really want is part of your icon altered. (At least, that's what it sounds like from your post...)

Something kind of like this:
 
Jason Robey
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll try that. Shaded polygons is definitely an efficiency issue right now that caching in an image/imageIcon might fix. I was able to make some improvement via VolitileImage double-buffering, but it remains a bit choppy on scrolling. I'll try the single icon approach and see if I can't cache some of the filled polygon drawing for components that don't move.
Jason
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!