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:
Looking for a better way to make JLabel fade in/out
-
13-06-2023 - |
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
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow