Question

Right now I have this (just increasing the label's alpha with small breaks in between):

            ae.getErrorLabel().setVisible(true);
            ae.getErrorLabel().setForeground(new Color(255, 0, 0, 0));
            ae.getErrorLabel().setBackground(new Color(0, 0, 0, 0));
            int alpha = 0;
            while (alpha < 255) {
                // wait-Methode
                long time0, time1;
                time0 = System.currentTimeMillis();
                do {
                    time1 = System.currentTimeMillis();
                } while ((time1 - time0) < 0.1 * 1000);
                // =====================================
                alpha += 25;
                if (alpha > 255)
                    alpha = 255;
                ae.getErrorLabel().setForeground(
                        new Color(255, 0, 0, alpha));
                ae.getErrorLabel().setBackground(
                        new Color(128, 128, 128, alpha));

This makes the JLabel "fade in".
But it looks kind of buggy and not really nice to look at.
Is there a better way to do it (which is not too complicated)?

Btw: It is swing, a simple JLabel on a smiple JPanel, I use this code in a separate thread that is run when you press a button

Was it helpful?

Solution

Animation is not an easy thing to implement correctly. For this reason is it better to use a tested library. One of the best is Trident. Here are related links:

https://kenai.com/projects/trident/pages/Home

https://github.com/kirillcool/trident

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top