2

I used to do the transparent background with javax.swing.JLabel this way:

lbl.setBackground(new Color(0, 0, 0, 0));.

But it doesn't work with java.awt.Label. Is there any simple way to make the label transparent?

Update:

public class SplashBackground extends Panel {

    private static final long serialVersionUID = 1L;

    private Image image = null;

    /**
     * This is the default constructor
     */
    public SplashBackground() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     */
    private void initialize() {
        image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/splash/splash.jpg"));
        this.setLayout(null);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(image != null) {
            g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
        }
    }

}

and

lbl= new Label();
lbl.setBackground(new Color(0, 0, 0, 0));
splashBackground = new SplashBackground();
splashBackground.add(appNameLabel, null);
bancer
  • 7,475
  • 7
  • 39
  • 58
  • 3
    Not sure why your JLabel needed to have its background set as it is not opaque by default, so you can set its background to anything and it will be transparent. So back to your question: why AWT and not Swing this go around? – Hovercraft Full Of Eels Jun 18 '11 at 23:57
  • 1
    @hovercraft, Not sure how I missed your comment, it was only made 3 hours before I made mine, +1. – camickr Jun 19 '11 at 04:19
  • @Hovercraft Full Of Eels: I think that's because I place the label in the custom panel (see update in the question). AWT because it loads faster. And that is for splash window. That is for a "learning" project. – bancer Jun 19 '11 at 10:18

2 Answers2

5

I can see why you do not want to load Swing, since it is for a splash. Sun/Oracle's own implementation of SplashScreen is AWT all the way.

Why not just use that existing class for your splash functionality?


As mentioned by camickr, see How to Create a Splash Screen for an example.

Splash using SplashScreen

Now that's what I'm talking about.


As to the labels, leave them out. Use FontMetrics or (better) TextLayout to determine the size/position of the text, then just paint it directly to the Image.

For an example of using the TextLayout class, see trashgod's answer to 'Java2D Graphics anti-aliased'.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1, see: http://download.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html for a woking example. – camickr Jun 19 '11 at 16:03
  • @Andrew, I don't see how this answers OP's original question, although it is a fine (and more correct) alternative... – mre Jun 19 '11 at 16:23
  • 1
    @mre: You are probably correct in your assessment. Sometimes I feel the OP *might be* asking the wrong question, and feel compelled to give the answer to the 'right question' as I best understand it. Of course, sometimes I get it horribly wrong. Actually - now I reread that title, strike 'probably correct' and make that 'correct'. – Andrew Thompson Jun 19 '11 at 16:46
  • I wanted to use my own implementation because it is hard to test java.awt.SplashScreen. I had to build MyApp.jar file every time I did changes. The splash window appeared only when I run jar file. – bancer Jun 19 '11 at 18:59
  • @bancer: "I had to build MyApp.jar file every time I did changes." Use Ant & a dev. machine with a little grunt and it ceases to be a problem to build a project for new runs. – Andrew Thompson Jun 19 '11 at 19:01
  • @Andrew: could you be more specific on that? I use Eclipse. Will I be able to click on "Run" button and run the project like if I would build jar file manually and run it? – bancer Jun 19 '11 at 19:20
  • @bancer: "Will I be able to click on "Run" button.." How would I know? I neither use nor support Eclipse. Separate question. – Andrew Thompson Jun 20 '11 at 06:39
-1

I used to do the transparent background with javax.swing.JLabel this way:

lbl.setBackground(new Color(0, 0, 0, 0));.

That doesn't do anything. A JLabel is transparent by default (ie setOpaque(false)). You may want to read Background With Transparency to understand how tranparency works with Swing.

I've never used a Label but if it works anything like a JLabel, then I would guess you override the isOpaque() method to return false.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288