Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with multiple transparent JPanels

843804Mar 19 2005 — edited Mar 20 2005
Hi,

I'm currently trying to implement some kind of message box that can fade in and out. This message box is a subclass of JComponent, the message it contains can be an arbitrary Swing-Component.
The message container and the message itself have the same background color. When fading, the (partly transparent) colors of the of the container and the message are added, which I do not want.
Here are two images to illustrate my problem:

What I have: http://www.inf.tu-dresden.de/~ab023578/javaforum/reality.gif
What I want: http://www.inf.tu-dresden.de/~ab023578/javaforum/wish.gif

Here is the code:
import java.awt.*;
import javax.swing.*;

public class Main {

    static float transparency = 0f;

    public static void main(String[] args) {

        JPanel jpBack = new JPanel() {
            protected void paintComponent(Graphics g) {
                Graphics2D g2d = (Graphics2D)g;
                g2d.clearRect(0, 0, getWidth(), getHeight());
                g2d.setColor(getBackground());
                AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency);
                g2d.setComposite(alpha);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        jpBack.setBackground(Color.WHITE);
        jpBack.setLayout(null);

        JPanel jpContainer = new JPanel();
        jpContainer.setBackground(Color.RED);
        jpContainer.setLayout(null);

        JPanel jpMessage = new JPanel();
        jpMessage.setBackground(Color.RED);
        
        JLabel jlMessage = new JLabel("MESSAGE");

        jpBack.add(jpContainer);
        jpContainer.add(jpMessage);
        jpMessage.add(jlMessage);

        jpContainer.setBounds(10, 10, 120, 100);
        jpMessage.setBounds(10, 10, 100, 50);


        JFrame frame = new JFrame();
        frame.setBounds(0, 0, 150, 150);
        frame.setBackground(Color.WHITE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(jpBack);
        frame.setVisible(true);
        for (double a = 0; true; a += 0.01D) {
            try {
                Thread.sleep(10);
            }
            catch (InterruptedException e) {}
            transparency = (float)Math.abs(Math.sin(a));
            jpBack.repaint();
        }
    }
}
I understand that the Porter-Duff-Rule SRC_OVER causes the container to be drawn over the message (or vice versa) and both of them over the background. I think what I need is a way to handle the whole jpContainer-Panel and all its subcomponents as a single source image. Is that possible?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 17 2005
Added on Mar 19 2005
7 comments
89 views