Painter Refactoring: Solving the Layering problem
I've discussed layering issues with Painters before, but we have yet
to actually solve the problem. I think it's time to finally attack
the problem head on. Rich and I have struggled to find what feels
like the *proper* solution and have come up with some ideas. However,
I don't want to put any pre-conceived notions in your heads so I'm
going to state the problem but not describe our solutions. My hope is
that others can provide some fresh insight to the problem.
What is the layering problem?
JXLabel, and other components which support Painters, allow the
developer to draw into the component with one or more Painters. But
where does the drawing go? Is the painter below or above the normal
painting of the component? What if the component has a background
(like JXPanel). Should the painting replace that drawing or augment
it? What if I want to add a painter on top of a component completely
to produce an overlay? What if I want to draw something between the
component's foreground and background? Will that even work on a
component with a native look and feel? And what about combining
painters? Should I be able to replace and/or augment any painters
already on the component, even if I don't know about them? And how
does setOpaque() affect things?
So you can begin to see how layering introduces lots of complicated
questions that don't have good answers. And we must also couple this
with the need to keep the API simple. It's okay to not enable every
possible behavior. If it comes down to it a developer can just
subclass if they want to.
So I think the solution must enable the following behavior. Can we
come up with a solution that does these things while still simple and
* Replace the background of a panel with custom drawing, allowing
content from below to shine through
* Add a dropshadow to the text of a label
* Change the background of a button to a translucent blue rounded
* Add a glow underneath the text of a button when rolled over
* use a stack of painters as the icon for a JLabel
* Add a small translucent icon on top of a textfield to indicate it
* Add a translucent red gradient over invalid textfields that already
have the previous effect
I think that's enough to get us started. So what do you think? Is
this all possible?
- Blasting forth in three part harmony!