stiko83
Messages postés2Date d'inscriptionmardi 13 février 2007StatutMembreDernière intervention14 février 2007
-
13 févr. 2007 à 12:30
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023
-
14 févr. 2007 à 19:48
salut,
je suis debutant en java, j aimerais savoir comment en utilisant SWT on peut ouvrir un fichier en tapant sur un bouton.et choisir les fichier en extensions .jpg, .bmp
stiko83
Messages postés2Date d'inscriptionmardi 13 février 2007StatutMembreDernière intervention14 février 2007 13 févr. 2007 à 15:17
ce que j 'aimerais c quand je tape sur le bouton un dialog image s'ouvre ou je peux choisir le fichier .jpg ou .bmp ke je voudrais ouvrir dans un CANVAS.
par contre est ce k il ya une fonction qui peut faire la meme chose que la fonction STRETCH() ou ZOOM() de PICTURE BOX de C#??
Twinuts
Messages postés5375Date d'inscriptiondimanche 4 mai 2003StatutModérateurDernière intervention14 juin 2023111 14 févr. 2007 à 19:48
Salut,
le seul moyen en java pure (sans api) de zoom
sur une image ou de faire un stretch c'est de te le coder en passant
par un BufferedImage exemple pour le zoom :
public static final Dimension MINIMUM_SIZE = new Dimension(10, 10);
/**
* Redimensionne une image
* @param source l'image a redimensionner
* @param size la nouvelle taille
* @return Retourne l'image redimentionne
*/
public static Image resize(Image source, Dimension size) throws OutOfMemoryError{
//rescale.
BufferedImage bimage = null;
boolean hasAlpha = hasAlpha(source);
//on test la taille pour pas lever une exception
Dimension msize = new Dimension(
size.width > MINIMUM_SIZE.width ? size.width : MINIMUM_SIZE.width,
size.height > MINIMUM_SIZE.height ? size.height : MINIMUM_SIZE.height);
// creation d'une image pour preparer le redimensionnement, en prenant en compte le fait
//qu'elle ai ou non une couleur de transparence
int type = BufferedImage.TYPE_INT_RGB;
if(hasAlpha)
type = BufferedImage.TYPE_INT_ARGB;
bimage = new BufferedImage(msize.width, msize.height, type);
Graphics2D g = (Graphics2D) bimage.getGraphics();
//on ajoute un petit rendu a l'image
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g.drawImage(source, 0, 0, msize.width, msize.height, null);
g.dispose();
return Toolkit.getDefaultToolkit().createImage(bimage.getSource());
}
/**
* 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();
}
exemple :
public static final int ZOOM_13 = 13;
public static final int ZOOM_25 = 25;
public static final int ZOOM_50 = 50;
public static final int ZOOM_75 = 75;
public static final int ZOOM_100 = 100;
public static final int ZOOM_200 = 200;
public static final int ZOOM_400 = 400;
public static final int ZOOM_800 = 800;
/**
* Redimensionne une image (ATTENTION TOUJOURS TRAVAILLER AVEC L'IMAGE D'ORIGINE)
* (Note la taille minimum de l'image est
MINIMUM_SIZE
)
* @param source l'image a redimensionner
* @param sourceImageSize la taille de l'image source
* @param percent pourcentage de zoom
* @return Retourne l'image redimentionne
* @see #ZOOM_13
* @see #ZOOM_25
* @see #ZOOM_50
* @see #ZOOM_75
* @see #ZOOM_100
* @see #ZOOM_200
* @see #ZOOM_400
* @see #ZOOM_800
*/
public static Image resize(Image source, Dimension sourceImageSize, int percent) throws OutOfMemoryError{
int w = sourceImageSize.width;
int h = sourceImageSize.height;
switch(percent){
case ZOOM_13:
case ZOOM_25:
case ZOOM_50:
case ZOOM_75:
int width = w - (w*percent/100);
int height = h - (h*percent/100);
return resize(source, new Dimension(
w > MINIMUM_SIZE.width ? w-= width : MINIMUM_SIZE.width,
h > MINIMUM_SIZE.height ? h-=height : MINIMUM_SIZE.height));
case ZOOM_100:
return source;
case ZOOM_200:
case ZOOM_400:
case ZOOM_800:
return resize(source, new Dimension(w+=w*percent/100, h+=h*percent/100));
default:
return source;
}
}
------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."