Reputation: 43
I've got an application which draws a background image
on a panel
, but to achieve the optimal UI, I need to set Component
backgrounds Transparent
:
I made every component transparent by using the UI Manager:
uimanager.put(Button, background(new color(0, 0, 0, 0);
<- something like that, this works perfectly, except..
onMouseOver
the component
redraws itself (I guess) and causes Artifacts.. How can I avoid this in the UIManager
?
(I created a class: uidefaults.java
with all the UIManager
settings)
Thanks in advance!!
Upvotes: 2
Views: 412
Reputation: 10143
Well, that is pretty simple - DO NOT use transparent background colors with components (any JComponent ancestor to be exact) that are OPAQUE.
To remove component background you don't need to set transparent color - just use this method:
component.setOpaque ( false );
This will hide component background and will also change component repaint strategy so it won't create any artifacts on repaint calls.
Also, if you still want to have semi-transparent background behind your component you can override paintComponent method like this:
JLabel label = new JLabel ( "Transparent background" )
{
protected void paintComponent ( Graphics g )
{
g.setColor ( getBackground () );
g.fillRect ( 0, 0, getWidth (), getHeight () );
super.paintComponent ( g );
}
};
label.setOpaque ( false );
label.setBackground ( new Color ( 255, 0, 0, 128 ) );
This will force label to hide its default background and also paint your own background (that depends on component's background property).
Upvotes: 3