1

Hope this question could emphasize more about the fading out effect of Jlabel (swing).

Certainly, yes... I already follow some guide and some answers given from This Link Thread, but mine is quite a bit different. It's not just only A text inside the JLabel, there's an image i put on.

I proceed to follow on the Thread Located out of stackoverflow. And yet, it gives me a fade out effect. But there's horrible thing occured; the white background.

How to solve this out?

I share the interface here... The First screenshot taken here is the earlier phase when the fade out have not occured yet. While, The Second screenshot taken here is the unwanted result i mentioned.

Tobe honest, I used the Trident Library to do animatiing; So, whenever the user click over the image it will execute this code;

Timeline tm = new Timeline(jll_btnProceed);
tm.addPropertyToInterpolate("intensity", 1.0f, 0.0f);
tm.setDuration(1000);
tm.play();

and... the JLabel itself, I used to override it using this source code;

/**
 *
 * @author Gumuruh
 */

public class JLabelFader extends JLabel {

    private float intensity = 1.0f;

    public void setIntensity(float intensity) {
        this.intensity = intensity;
        this.setOpaque(false);
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        final Composite oldComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.SrcOver);
        final Color c = getBackground();
        final Color color = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255 * (1.0f - intensity)));
        g2.setColor(color);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.setComposite(oldComposite);
    }
}

My hand and my head can't stop for making this trully solved. Thus I tried to follow up some example from the Java Filthy Rich Client ebook and then applying the source code given below, but first I need to COMMENT the protected void paint(Graphic g) method written above, and simply adding this source code;

private BufferedImage buttonImage = null;

    public void paint(Graphics g) {

        // Create an image for the button graphics if necessary
        if (buttonImage == null || buttonImage.getWidth() != getWidth()
                || buttonImage.getHeight() != getHeight()) {
            buttonImage = getGraphicsConfiguration().
                    createCompatibleImage(getWidth(), getHeight());
        }
        Graphics gButton = buttonImage.getGraphics();
        gButton.setClip(g.getClip());

        //  Have the superclass render the button for us
        super.paint(gButton);

        // Make the graphics object sent to this paint() method translucent
        Graphics2D g2d = (Graphics2D) g;
        AlphaComposite newComposite =
                AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity);
        g2d.setComposite(newComposite);

        // Copy the button's image to the destination graphics, translucently
        g2d.drawImage(buttonImage, 0, 0, null);

    }

in which at the end... giving me nice fade out effect. But At first, it gave me the 2nd horrible effect which is BLACK BACKGROUND rendered first. Can't believe me?? Okay, Here is First screen shot AFTER applying code from ebook. and here is the nice fade out effect result.

If there's somebody telling me; "YOUR PNG IS NOT TRANSPARENT!". Please, dont say like that. Because, I followed the creation of PNG inside the Photoshop nicely using this Tut.

Now, My head's spinned, but my heart laughed and can't handle it over. OMG. Geeezzz....!

And the New Stories begun... * UPDATED FROM HERE AND BELOW *

Ehm, depply thank you very much to our friend called... MKorbel, from his thread given at this link. Providing a clear example of fading out effect the Swing JButton and I tried to implement it into my JLabel, and violaaa...!! IT works. Let's give a big clap for MKorbel. :D

SO anyway, how could I fix the earlier code? Pretty simple, just COMMENT the Paint() method, and use again the paintComponent() method and it should be overriden with the new source code below;

@Override
    public void paintComponent(java.awt.Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, intensity));
        if (rectangularLAF && isBackgroundSet()) {
            Color c = getBackground();
            g2.setColor(c);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        super.paintComponent(g2);
        g2.dispose();
    }

Now the JLabel become easy to be changed with its intensity -variable. Fading out and Fading in is accomplished.

Okay. Now everything seems "OKAY". BUt hold a moment, there's something strangely occured again here. Have you noticed it? Hmmm.... I tried to give a Mouse Event (Hover On) into the JLabel that we override the paintComponent() method discussed earlier.With this line of code;

myJLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

Logically, it should change the Cursor when Hover ON. But, Here comes another strange effect. (Really sorry, but this is stil the continue of the main case). The strange effect now is the Mouse Cursor can't be changed when we Hover On the Jlabel. It still can't change the Cursor. Seems the paintComponent() method effecting the way Cursor react. Is that true?

Community
  • 1
  • 1
gumuruh
  • 2,544
  • 4
  • 33
  • 56
  • queston -> what were happends if you tried to set/change Alpha translucency or transparency without Substance/Trident libraries – mKorbel Aug 08 '11 at 07:54
  • change the alpha translucency, mKorbel? which one? I'm actually changing the color via "intensity" variable ---> intended to give an alpha color. – gumuruh Aug 09 '11 at 03:42
  • 1
    please check my thread http://stackoverflow.com/questions/6992154/java-transparent-panels-custom-effect-on-panels/6992259#6992259 , only if that will be works as you expected then you can apply Trident libs. – mKorbel Aug 09 '11 at 06:37
  • hey, mKorbel...! I just realized it today, since it's OKay. and you saved my upper topics. There's still something unwanted effect. The Cursor seems can't be applied any longer if I tried to implement your code (paintComponents) algorithm... :( – gumuruh Aug 13 '11 at 05:55
  • that was very academic tread, now you are two options, edit this thread or start a new, but with runnable code that shows what you are tried .... – mKorbel Aug 13 '11 at 06:06
  • ya MKorbel... the thread is now updated. It is very strange... previously the fade out effect, now the cursor... OMG. Hopefully there's somebody could give us a hint about this. :) – gumuruh Aug 15 '11 at 04:06
  • not clear for me :-), problem is with JLabel&Cursor if mouse hover-over JLabel, or you can't ba able to change Cursor, look for MouseMotionListener + get Rectangle from JLabel on the screen :-) – mKorbel Aug 15 '11 at 06:14
  • @mKorbel : ya The Cursor can't be changed. I already give the mouse event on Hover and put the code, but seems it failed. But If i use the common JLabel, and not the Above-Fader-JLabel, everything works fine. :( – gumuruh Aug 15 '11 at 06:46
  • `Cursor` works for me http://stackoverflow.com/questions/6051755/java-wait-cursor-display-problem/6060678#6060678 – mKorbel Aug 15 '11 at 07:07
  • mKorbel, thanks for the posted thread. But, it doesn't effecting the JLabel above.Anyway, I already update my JDK but seems still have the problem... hmmmm.... I wonder why. – gumuruh Aug 16 '11 at 05:38
  • to try based you code on official tutorial and rest on http://tips4java.wordpress.com/?s=transparent – mKorbel Aug 16 '11 at 06:49
  • @mKorbel: okay, okay.... right away to go there.... :D – gumuruh Aug 21 '11 at 08:03

0 Answers0