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
| package com.tonprojet.splashscreen;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JWindow;
import org.apache.log4j.Logger;
import com.tonprojet.utilitaire.PathTools;
public class SplashScreen extends JWindow
{
Logger log = Logger.getLogger(this.getClass());
private BufferedImage image;
public SplashScreen(String fileName, long time)
{
super();
// On recherche d'abord l'image sur le disque
// Si on ne la trouve pas, on regarde dans le classpath
URL urlImage = null;
// Recherche sur le disque
// Il faut préciser que l'image est un chemin relatif, d'où le . devant
File fileImage = new File(PathTools.getPath("."+fileName));
if (fileImage != null && fileImage.exists())
{
try
{
urlImage = fileImage.toURL();
}
catch (MalformedURLException ignore)
{
}
}
if (urlImage == null)
{
// Recherche dans le classpath
urlImage = getClass().getResource(PathTools.getPath(fileName));
}
try
{
image = ImageIO.read(urlImage);
setSize(new Dimension(image.getWidth(), image.getHeight()));
setLocationRelativeTo(null);
setVisible(true);
}
catch (IOException exception)
{
log.error("Erreur lors de l'affichage du splash screen", exception);
}
if (time > 0)
{
TimerTask dispose = new TimerTask()
{
public void run()
{
dispose();
}
};
Timer timer = new Timer();
timer.schedule(dispose, time);
try
{
Thread.sleep(time);
}
catch (InterruptedException exception)
{
log.error("Erreur lors de l'affichage du splash screen", exception);
}
}
}
public SplashScreen(String file)
{
this(file, 0);
}
public void paint(Graphics g)
{
if (image.getColorModel().hasAlpha())
{
try
{
Robot robot = new Robot();
BufferedImage fond = robot.createScreenCapture(getBounds());
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(fond, 0);
tracker.waitForAll();
g.drawImage(fond, 0, 0, null);
}
catch (AWTException e)
{
log.error("Erreur lors de l'affichage du splash screen", e);
}
catch (InterruptedException exception)
{
log.error("Erreur lors de l'affichage du splash screen", exception);
}
}
g.drawImage(image, 0, 0, null);
}
} |
Partager