Charger trois images sur disque
Bonjour à tous je voudrais charger trois images sur disque
mais je bloque encore à la première image
le code ci dessous devrait écrire l'image sur disque et l'afficher
j'après mes test il le stocke bien mais affiche une cadre noir je ne comprend pas pourquoi
merci de m'aider à comprendre
Code:
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
|
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class AfficheImage extends JPanel {
BufferedImage imageAAfficher,imageAStocker;
JFileChooser fc;
JDialog info;
String format;
File image;
int i=1,larg,haut;
private static final long serialVersionUID = 1L;
boolean affiche;
public AfficheImage() {
super();
}
public void afficher (boolean val)
{
fc = new JFileChooser();
int vals = fc.showOpenDialog(this);
if (vals==JFileChooser.CANCEL_OPTION)
image=null;
else
{
image=fc.getSelectedFile();
format = (image.toString()).substring((image.toString()).lastIndexOf('.')+1);
//Fonction pour ecrire sur disque
SauveImage(i,image.toString());
//Image suivante
i++;
affiche = val;
}
try {
imageAAfficher = ImageIO.read(image);
} catch (IOException e) {
e.printStackTrace();
}
repaint();
}
private void SauveImage(int val, String img) {
image = new File(img);
BufferedImage bi = new BufferedImage(300, 300, val);
format = img.substring(img.lastIndexOf('.')+1);
try{
boolean success = ImageIO.write(bi, format, image);
if (!success) {
JOptionPane.showMessageDialog(new JFrame(), "Ecriture impossible:"+format);
}else{
JOptionPane.showMessageDialog(new JFrame(), "ok");
}
}catch(Exception e)
{
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if((imageAAfficher != null) && (affiche))
{
g.drawImage(imageAAfficher, 5,5,300, 300, null);
}
}
} |
l'appel est fait par
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public void actionPerformed(ActionEvent ev) {
Object src = ev.getSource();
if (src == MenuOuvrir)
{ //panneauGauche.afficher(true);
System.out.println(ev.getActionCommand());
PanelDeGauche.afficher(true);
}
if (src == MenuQuitter)
{
System.exit(0);
}
} |