user285594
user285594

Reputation:

How to make the AWT component transparent on progressive background image? Used Window(), Panel(), Button() but failed

How can i make AWT component transparent when the background is progressive image?

Note: AWT used only, where progressive Window() 5 frames/per second. Panel() is not getting transparent now while using new Color(255,0,0,0). How to make the panel transparent?

enter image description here

AWT? here:

public class 888 extends Window 
{
    private Button button;

    public 888() 
    {
        super(new Frame());
        // Transparent failed
        getOwner().setBackground(new Color(255, 0, 0, 0) );

        this.setLayout (new BorderLayout ());

        button = new Button("close");

        button.setBackground(Color.RED);
        button.setSize(200,200);
        button.setLocation(0,20);

        this.add("North", button);

        Panel p = new Panel();
        //p.setOpaque(false);
        p.setSize(200,200);
        p.setLocation(400,400);
        // Transparent failed
        p.setBackground(new Color(255,0,0,0));
        p.add("Left", new Button("Test"));
        this.add("North", p);

        //AWTUtilities.setWindowOpacity(this, 0.2f); // Error in Linux

    }


    public static void main(String[] args) 
    {
        Window j = new 888();
        j.setVisible(true);
        //j.setBackground( new Color(255, 0, 0, 0) );
    }

}

SWing?: here

public class 888 extends JWindow 
{
    private JButton button;

    public 888() 
    {
        //super(new Frame());
    // Transparent failed
        getOwner().setBackground(new Color(255, 0, 0, 0) ); 

        this.setLayout (new BorderLayout ());

        button = new JButton("close");

        button.setBackground(new Color(255,0,0,255));
        button.setSize(200,200);
        button.setLocation(0,20);

        this.add("North", button);

        JPanel p = new JPanel();
        //p.setOpaque(false);
        p.setSize(200,200);
        p.setLocation(400,400);
        // Transparent failed
        p.setBackground(new Color(255,0,0,0));
        p.add("Left", new Button("Test"));
        this.add("North", p);

        //AWTUtilities.setWindowOpacity(this, 0.2f); // Error in Linux

    }


    public static void main(String[] args) 
    {
        JWindow j = new 888();
        j.setVisible(true);
        //j.setBackground( new Color(255, 0, 0, 0) );
    }

}

Note: None really works, with Swing i have problem with JButton, because when the JWindow get refreshed the JButton become invisible. With AWT when i use Button and Panel then none can set its transparency.

How do i solve it? For progressive background image Panel or Button to make transparent?

Summary:

I came to a point now after doing many tests, which is not available anywhere else. Mostly in a progressive scan Window() or JWindow(). You better use Awt for (no transparent objects), and Swing for (transparent objects). Do not make a conclusion like many not to use Awt and Swing, you have to be smart to mix it, else gonna end up with endless summer of 69 :)

Reference:

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html

Upvotes: 1

Views: 3775

Answers (1)

camickr
camickr

Reputation: 324197

This is your second question on this topic and I still can't tell what you are doing. You keep talking about a background image, but no where in your code do you show the code for the image.

Yes there are problems when you use setColor with an alpha value of 0. You should NOT do this. The proper solution is to just make the component non-opaque.

See Background With Transparency for more information.

Or maybe Background Panel is what you are looking for.

Also you usage of constraints is old:

this.add("North", button); 

First of all you should not hard code the constraint value. Secondly if you read theAPI you will see that you should be using:

this.add(button, BorderLayout.NORTH); 

and when using layout managers you don't set the size and location.

Upvotes: 2

Related Questions