Hallo!

Hier mal ein Beispiel für einen SplashScreen der zunächst ein paar Sekunden angezeigt und anschließend "sanft" in den Hintergrund gefaded wird und danach die eigentliche Anwendung startet.

Code java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * 
 */
package de.tutorials;
 
import java.awt.AWTException;
import java.awt.AlphaComposite;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JWindow;
 
/**
 * @author Tom
 * 
 */
public class FadingSplashScreenExample {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        FadingSplashScreen fadingSplashScreen = new FadingSplashScreen(
                "E:/eclipse/3.2.1/eclipse/plugins/org.eclipse.platform_3.2.0.v20060601/splash.bmp",
                32,
                3);
        fadingSplashScreen.setVisible(true);
        fadingSplashScreen.setFinishedCallback(new Runnable() {
            public void run() {
                JFrame frm = new JFrame("Application");
                frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frm.setSize(800,600);
                frm.setLocationRelativeTo(null);
                frm.setVisible(true);
                
            }
        });
        fadingSplashScreen.startFading();
 
    }
 
    static class FadingSplashScreen extends JWindow {
        String pathToSplashScreenImage;
 
        int numberOfSecondsToShow;
 
        BufferedImage splashScreenImage;
 
        BufferedImage fadedSplashScreenImage;
 
        BufferedImage screenShot;
 
        ExecutorService fadingExecutor;
 
        Runnable fadingCommand;
 
        Runnable finishedCallback;
 
        int numberOfFadingSteps;
 
        /**
         * @param pathToSplashScreenImage
         * @param numberOfSecondsToShow
         * @param
         */
        public FadingSplashScreen(String pathToSplashScreenImage,
                int numberOfFadingSteps, int numberOfSecondsToShow) {
            super();
            this.numberOfFadingSteps = numberOfFadingSteps;
            this.pathToSplashScreenImage = pathToSplashScreenImage;
            this.numberOfSecondsToShow = numberOfSecondsToShow;
            init();
        }
 
        public void setFinishedCallback(Runnable finishedCallback) {
            this.finishedCallback = finishedCallback;
        }
 
        public void startFading() {
            fadingExecutor.execute(fadingCommand);
        }
 
        private void init() {
            try {
                this.splashScreenImage = loadSplashScreenImage();
                this.screenShot = makeScreenShot(
                        splashScreenImage.getWidth(),
                        splashScreenImage.getHeight());
                setSize(splashScreenImage.getWidth(), splashScreenImage
                        .getHeight());
                setLocationRelativeTo(null);
                this.fadingCommand = new Runnable() {
                    public void run() {
 
                        fadedSplashScreenImage = splashScreenImage;
                        repaint();
 
                        try {
                            TimeUnit.SECONDS.sleep(numberOfSecondsToShow);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
 
                        float alpha = 1.0F;
                        float alphaFactor = 1.0F / numberOfFadingSteps;
                        for (int i = 0; i < numberOfFadingSteps; i++) {
                            float currentAlpha = alpha - i * alphaFactor;
                            fadedSplashScreenImage = fadeSplashImage(AlphaComposite
                                    .getInstance(
                                            AlphaComposite.SRC_OVER,
                                            currentAlpha));
                            //System.out.println(currentAlpha);
                            repaint();
                            try {
                                TimeUnit.MILLISECONDS.sleep(50);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        setVisible(false);
                        dispose();
                        finishedCallback.run();
                    }
                };
                this.fadingExecutor = Executors.newSingleThreadExecutor();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        private BufferedImage fadeSplashImage(AlphaComposite composite) {
            BufferedImage bufferedImage = new BufferedImage(
                    splashScreenImage.getWidth(),
                    splashScreenImage.getHeight(),
                    BufferedImage.TYPE_INT_ARGB);
 
            Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
            g.drawImage(screenShot, 0, 0, null);
            g.setComposite(composite);
            g.drawImage(splashScreenImage, 0, 0, null);
 
            return bufferedImage;
        }
 
        private BufferedImage makeScreenShot(int width, int height)
                throws AWTException {
            Robot robot = new Robot();
            DisplayMode displayMode = GraphicsEnvironment
                    .getLocalGraphicsEnvironment()
                    .getDefaultScreenDevice()
                    .getDisplayMode();
 
            return robot.createScreenCapture(new Rectangle(
                    displayMode.getWidth() / 2 - width / 2,
                    displayMode.getHeight() / 2 - height / 2,
                    width,
                    height));
        }
 
        private BufferedImage loadSplashScreenImage() throws IOException {
            return ImageIO.read(new File(pathToSplashScreenImage));
        }
 
        public void paint(Graphics g) {
            g.drawImage(fadedSplashScreenImage, 0, 0, null);
        }
    }
}

Gruß Tom