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);
}
}
} |
Partager