Ecco un esempio che mostra 3 pannelli sovrapposti (con JLayeredPane) in cui vengono usati dei colori con canale alpha.
codice:
import java.awt.*;
import java.net.*;
import javax.swing.*;
public class TestFrame extends JFrame
{
private JLayeredPane layeredPane;
public TestFrame ()
{
super ("Test Frame");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize (300, 300);
layeredPane = new JLayeredPane ();
getContentPane ().add (layeredPane);
try {
ImageIcon icon = new ImageIcon (new URL ("http://img231.imageshack.us/img231/4627/bluebackgroundvb2.jpg"));
ImgPanel imgPanel = new ImgPanel (icon.getImage ());
MyPanel panel1 = new MyPanel (new Color (255, 255, 0, 64));
MyPanel panel2 = new MyPanel (new Color (255, 0, 0, 64));
imgPanel.setBounds (0, 0, 300, 300);
panel1.setBounds (120, 20, 100, 100);
panel2.setBounds (60, 60, 140, 140);
layeredPane.add (imgPanel, new Integer (0));
layeredPane.add (panel1, new Integer (1));
layeredPane.add (panel2, new Integer (2));
}
catch (Exception e) {
System.out.println (e);
}
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
TestFrame f = new TestFrame ();
f.setVisible (true);
}
});
}
}
class ImgPanel extends JPanel
{
private Image img;
public ImgPanel (Image img)
{
this.img = img;
}
public void paintComponent (Graphics g)
{
g.drawImage (img, 0, 0, this);
}
}
class MyPanel extends JPanel
{
private Color c;
public MyPanel (Color c)
{
this.c = c;
}
public void paintComponent (Graphics g)
{
g.setColor (c);
g.fillOval (0, 0, getWidth(), getHeight());
}
}