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
| public class Module extends BufferedImage {
Module(String imageName) throws IOException {
this(ImageFactory.getImage(imageName));
}
Module(BufferedImage image) throws NullPointerException {
super(image.getWidth(), image.getHeight(), image.getType()}
ImageUtilities.copyImage(image, this);
}
}
public final class ImageFactory {
public static BufferedImage image getImage(String filename) throws IOException {
URL url = ImageFactory.class.getResource(filename);
// OU
//File = new File(filename);
//URL url = file.toURL();
// Celon qu'on utilise une ressource de l'application ou un fichier sur le disque dont on connait le nom "en dur".
BufferedImate result = ImageIO.read(url);
result = ImageUtilities.toCompatibleImage(result);
return result;
}
}
public final class ImageUtilities {
public static BufferedImage toCompatibleImage(Image source) {
BufferedImage result = null;
if ((source != null) && (source.getWidth(null) != 0) && (source.getHeight(null) !=0) {
result = (BufferedImage)copyImage(source, null);
}
return result;
}
public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
BufferedImage result = null;
if ((width != 0) && (height !=0) {
GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
result = GraphicsConfiguration.createCompatibleImage(width, height, transparency);
}
return result;
}
public Image copyImage(Image source, Image destination) throws NullPointerException {
if ((source != null) && (source.getWidth() != 0) && (source.getHeight() !=0) {
if (destination == null) {
destination = makeCompatibleImage(source.getWidth(null), source.getHeight(null), source.getTransparency());
}
Graphics graphics = destination.getGraphics();
try {
graphics.drawImage(source, 0, 0, null);
}
finally {
graphics.dispose();
}
}
return destination;
}
} |
Partager