-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAlphaContainer.java
More file actions
42 lines (39 loc) · 1.32 KB
/
AlphaContainer.java
File metadata and controls
42 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JComponent;
/**
* A wrapper Container for holding components that use a background Color
* containing an alpha value with some transparency.
*
* A Component that uses a transparent background should really have its
* opaque property set to false so that the area it occupies is first painted
* by its opaque ancestor (to make sure no painting artifacts exist). However,
* if the property is set to false, then most Swing components will not paint
* the background at all, so you lose the transparent background Color.
*
* This components attempts to get around this problem by doing the
* background painting on behalf of its contained Component, using the
* background Color of the Component.
*/
public class AlphaContainer extends JComponent
{
private JComponent component;
public AlphaContainer(JComponent component)
{
this.component = component;
setLayout( new BorderLayout() );
setOpaque( false );
component.setOpaque( false );
add( component );
}
/**
* Paint the background using the background Color of the
* contained component
*/
@Override
public void paintComponent(Graphics g)
{
g.setColor( component.getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
}
}