|
|
|
|
Configuration: Windows XP Internet Explorer 7.0
Salut!
J'ai créé il y a quelque temps une petite classe utilitaire qui permet d'afficher un splashscreen. En voici le code:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
public class SplashWindow extends JWindow {
private static SplashWindow instance;
private long minDuration;
private SplashWindow(String imagePath, long minDuration) {
super();
this.minDuration = minDuration;
Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
if (imagePath != null && !imagePath.equals("")) {
ImageIcon img = new ImageIcon(imagePath);
int imgHeight = img.getIconHeight();
int imgWidth = img.getIconWidth();
int imgX = (screenDimension.width - imgWidth) / 2;
int imgY = (screenDimension.height - imgHeight) / 2;
JLabel jl_img = new JLabel(img);
jl_img.setPreferredSize(new Dimension(imgWidth, imgHeight));
getContentPane().add(new JLabel(img));
setLocation(imgX,imgY);
} else {
setLocation(screenDimension.width / 2, screenDimension.height / 2);
}
pack();
setVisible(true);
long tEnd = System.currentTimeMillis() + minDuration;
while (System.currentTimeMillis() < tEnd) {
}
}
public static SplashWindow getInstance(String imagePath, long minDuration) {
if (instance == null) {
instance = new SplashWindow(imagePath, minDuration);
}
return instance;
}
public static SplashWindow getInstance(String imagePath) {
if (instance == null) {
instance = new SplashWindow(imagePath, 0);
}
return instance;
}
public static SplashWindow getInstance() {
return getInstance(null, 0);
}
public void end() {
setVisible(false);
dispose();
}
}
Tu en crées une instance en appelant la méthode statique getInstance() avec comme paramètre le chemin de l'image à afficher et - pour l'autre méthode statique getInstance() - le temps minimum d'affichage de l'image (bloquant pou le reste du code). Pour le faire diparaître, appelle la méthode end() sur l'instance. Amélioration à apporter (je t'en laisse le soin): transformer la classe en Thread ou Runnable ;-) HackTrack |
Voila un bel exemple de code crado |