1

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

  • 1
    Why does you question have a Swing tag, when you only want this for AWT? The obvious amswer wouuld be to use Swing components which do support opacity but it appears you don't want to use Swing. Also you talk about a background image, but your posted code doesn't use a background image. – camickr Jul 16 '11 at 14:39
  • I agree with camickr: why not use Swing for this? Are there constraints not mentioned that force the use of AWT? – Hovercraft Full Of Eels Jul 16 '11 at 14:41
  • 1
    @camickr: no its not. button.setBackground(new color(255,0,0,0)); as JButton, JPanel, JWindow failed. In progressive background image, on mouse over it shows and when the background is refreshed it lost button. –  Jul 16 '11 at 14:42
  • 1
    @Hovercraft Full Of Eels: already done. Problem with JButton –  Jul 16 '11 at 14:43
  • Does your 888 class even compile? – Hovercraft Full Of Eels Jul 16 '11 at 14:55

1 Answers1

2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • please see above my update. I tried 100% AWT and 100% Swing did not worked. And i also tried AWTUtilities.setWindowOpacity(this, 0.5f); Which has bug in Linux not working with latest JDK/JRE. –  Jul 16 '11 at 14:49
  • So I still don't understand your question. Your question is about making a "component transparent" or "how to make a panel transparent", not a JFrame or a JWindow. Are you trying to find a solution for a JWindow, if so then why does your SSCCE have regular components on the window. Are you trying to find a Linux fix. If so, then state that in your question. Be specific we can't guess. – camickr Jul 16 '11 at 14:58
  • When i am using Swing and JPanel() i can make transparent. But JButton() lost its visibility only. But when i use AWT and Panel() i cant make it transparent. How can i use AWT and Transparent Panel() is my question, where Button() works and does not lose its visibility. –  Jul 16 '11 at 15:07
  • "When i am using Swing and JPanel() i can make transparent. But JButton() lost its visibility only.". I don't understand what you are saying. Your code uses a JButton and a Button which is confusing. You still haven't tried the suggestions I gave you. I still don't see an image anywhere in your code. Post your post your SSCCE that uses the sugestions I have already given you. – camickr Jul 16 '11 at 15:30
  • its working, for me, i solved it. I have to use AWT and SWING both for progressive scan Window() or JWindow(). Thanks and the links that you posted i also tried but did not helped. –  Jul 16 '11 at 16:21
  • I still don't understand what you are doing. Why don't you post your working code for other to learn from? – camickr Jul 16 '11 at 17:13
  • When i do repaint() and pause or stop my image progressive scan to Window() or JWindow(). Then Swing button remains visible, is there any way to refresh Swing buttons? –  Jul 16 '11 at 20:37
  • I'm not going to chase bits and pieces of code all over the web. If you want help then post a proper SSCCE. – camickr Jul 17 '11 at 03:37