static Tableau frame ;
public static void main ( String[] args ) {
frame = new Tableau() ;
frame.setVisible(true) ;
}
}
class Tableau extends JFrame {
//private static final long serialVersionUID = -1654172985512850048L;
static final Dimension screensize =
Toolkit.getDefaultToolkit().getScreenSize();
static final int largeurEcran = screensize.width;
static final int hauteurEcran = screensize.height + 2;
// static final int largeurEcran = 1024;
//static final int hauteurEcran = 768;
PhotoCanvas photo = new PhotoCanvas("lena.jpg") ;
l = photo.getWidth();
h = photo.getHeight();
setBounds((largeurEcran - l) / 2,
(hauteurEcran - h) / 2, l, h);
pane.add(photo);
}
}
class PhotoCanvas extends JPanel {
private static final long serialVersionUID = -425196446497911425L ;
Image img ;
public PhotoCanvas ( String s ) {
setSize (400,300 );
img = getToolkit().getImage(s) ;
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try {
tracker.waitForAll();
} catch(InterruptedException e) {
System.err.println(e);
}
setSize(img.getWidth(this),
img.getHeight(this));
}
public static BufferedImage contour(BufferedImage img){
float[] filter = { 0f, -1f, 0f, -1f, 4f, -1f, 0f, -1f, 0f };
Kernel kernel = new Kernel(3, 3, filter);
ConvolveOp op = new ConvolveOp(kernel);
return op.filter(img, null);
}
public void paintComponent(Graphics g) {
try{
g.drawImage(contour(createBufferedImag(img)),0,0,this);
}catch(OutOfMemoryError e){
System.err.println("Pass assez de mémoire !!!");
}
}
/**
* Transforme une image en bufferedImage
* @param image l'image a transformer
* @return Retourn l'image convertie ou null (si l'image source ete null)
*/
public static BufferedImage createBufferedImage(Image img) throws OutOfMemoryError {
if(img == null) return null;//si l'image est null pas besoin de continuer
if (img instanceof BufferedImage)//si l'image est deja une instance de BufferedImage on la retourne
return (BufferedImage) img;
// On s'assure que l'image soit bien chargee
img = new ImageIcon(img).getImage();
//Determine si l'image contient ou non un pixel de transparence
boolean hasAlpha = hasAlpha(img);
int type;//type de couleur
// On creer un BufferedImage ayant un format compatible avec l'environement graphique du pc
BufferedImage bi = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
//Maintenant on determine si le nouveau BufferedImage aura ou non un pixel de transparence
type = Transparency.OPAQUE;
if (hasAlpha) type = Transparency.BITMASK;
// Creation de notre nouveau BufferedImage
bi = ge.getDefaultScreenDevice()
.getDefaultConfiguration().
createCompatibleImage(
img.getWidth(null), img .getHeight(null), type);
} catch (HeadlessException e) {
// Le systeme n'as pas d'ecran ?
}
if (bi == null) {
// Si le BufferedImage precedement creer est null on en creer un avec un model par defaut
type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) type = BufferedImage.TYPE_INT_ARGB;
bi = new BufferedImage(img.getWidth(null), img.getHeight(null), type);
}
// On copie l'image dans le BufferedImage
Graphics g = bi.createGraphics();
// maintenant on la dessine
g.drawImage(img, 0, 0, null);
g.dispose();
return bi;
}
/**
* Test si une image contient ou non un canal alpha
* @param image l'image a tester
* @return retourne vrai si elle en contien un
*/
public static boolean hasAlpha(Image image) throws OutOfMemoryError {
// si l'image et deja un BufferedImage alors pas besoin de checker le model de couleur
if (image instanceof BufferedImage)
return ((BufferedImage) image).getColorModel().hasAlpha();
//utilisation d'un 'grabber' pour retrouver le model de couleur de l'image
//normalement le grab du pixel en haut a gauche est suffisant.
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
if(!pg.grabPixels()) return false;
} catch (InterruptedException e) {
}
// on check si le model de couleur correspond ou non a un chanel alpha
return pg.getColorModel().hasAlpha();
}