PRBL affichage image avec jfilechooser
J'ai un ptit prbl que je n'arrive pas à remerdiér.
J'ai envi de mettre un jfilerchooser et un jpanel dans la meme fenetre l'un au nord et l autre au sud.Des qui selectionne une image,l image apparait dans le jpanel,
Voici mon code
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/*
* affichagejchooser.java
*
* Created on 28 juin 2006, 16:56
*/
package projetgroupama;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.beans.*;
import javax.swing.border.*;
import java.io.File;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.widget.ScrollingImagePanel;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.Interpolation;
/**
*
* @author Administrateur
*/
public class affichagejchooser extends javax.swing.JFrame {
/** Creates new form affichagejchooser */
private FenApplication f;
private affichagejchooser a;
private String nom;
RenderedOp im1;
private ScrollingImagePanel imagePanel;
private JPanel p;
public affichagejchooser(FenApplication f,affichagejchooser a) {
this.f=f;
this.a=a;
this.nom="";
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p=new JPanel();//CONTAINER
jPanel1 = new JPanel();
BorderLayout bord = new BorderLayout();
p.setLayout(bord);
setVisible(true);
im1 = LoadTiff("C:\\Documents and Settings\\Administrateur\\Bureau\\ProjetGroupama\\ProjetGroupama\\src\\projetgroupama\\images\\img_noimage.tif");
DisplayTiff(im1);
repaint();
JFileChooser jFileChooser2 = new JFileChooser();
jFileChooser2.setFileSelectionMode(jFileChooser2.FILES_ONLY);
jFileChooser2.setDialogType(jFileChooser2.OPEN_DIALOG);
jFileChooser2.setDialogTitle("Ouvrir une image");
jFileChooser2.setApproveButtonText("Choix du fichier...");
jFileChooser2.setMultiSelectionEnabled(false);
JFileFilter filtre = new JFileFilter();
filtre.addType("tif");
filtre.addType("jpg");
filtre.setDescription("Fichiers image :.tif, .jpg");
jFileChooser2.addChoosableFileFilter(filtre);
jFileChooser2.setAccessory(new FilePreviewer(jFileChooser2,jPanel1));
jFileChooser2.setAcceptAllFileFilterUsed(false);
p.add("North",jFileChooser2);
p.add("Center",jPanel1);
setContentPane(p);
if (jFileChooser2.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
String fichier =jFileChooser2.getSelectedFile().getAbsolutePath();
f.changementimage(fichier);
}
}
public affichagejchooser(){}
public void DisplayTiff(RenderedOp image) {
imagePanel = new ScrollingImagePanel(image,p.getWidth()/2,p.getHeight()/2);
jPanel1.add(imagePanel);
repaint();
}
public RenderedOp LoadTiff(String nom) {
String image =nom;
RenderedOp im = JAI.create("fileload", image);
Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
ParameterBlock pb = new ParameterBlock();
pb.addSource(im); // The source image
pb.add(0.5F); // The x origin
pb.add(0.5F); // The y origin
pb.add(0.5F); // The x translation
pb.add(0.5F); // The y translation
pb.add(interp); // The interpolation
im = JAI.create("scale",pb,null);
return im;
}
// Variables declaration - do not modify
private javax.swing.JFileChooser jFileChooser2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
} |
jFileChooser2.setAccessory(new FilePreviewer(jFileChooser2,jPanel1)); permet de des qui selectionne un fichier,je fais un add dans le jpanel
Voici le code
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
package projetgroupama;
import java.beans.*;
import javax.swing.border.*;
import java.awt.*;
import javax.swing.*;
import java.io.File;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.media.jai.widget.ScrollingImagePanel;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.Interpolation;
public class FilePreviewer extends JComponent implements PropertyChangeListener {
//Variables globales
ImageIcon thumbnail = null;
RenderedOp im1;
private ScrollingImagePanel imagePanel;
private JPanel jpanelimages;
private String nom;
private JPanel j;
private JFileChooser fc;
/**Constructeur*/
public FilePreviewer(JFileChooser fc,JPanel jpanel) {
this.fc=fc;
this.j=jpanel;
setPreferredSize(new Dimension(200, 100));
fc.addPropertyChangeListener(this);
setBorder(new BevelBorder(BevelBorder.LOWERED));
this.nextFocus();
}
/**Méthode permettant le chargement des images*/
public void loadImage(File f) {
this.nom=f.getPath();
if (f == null) {
thumbnail = null;
} else {
ImageIcon tmpIcon = new ImageIcon(f.getPath());
if(tmpIcon.getIconWidth() > 90) {
thumbnail = new ImageIcon(
tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
} else {
thumbnail = tmpIcon;
}
}
affich(nom);
}
/**Méthode appelé dans le cas d'un changement de fichier sélectionné*/
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if(prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
if(isShowing()) {
loadImage((File) e.getNewValue());
repaint();
}
}
}
public void affich(String noms){
this.nom=noms;
this.nom= nom.replace("\\","\\\\");
this.im1 = LoadTiff(nom);
DisplayTiff(im1);
repaint();
}
public void DisplayTiff(RenderedOp image) {
if(imagePanel == null) {
imagePanel = new ScrollingImagePanel(image,this.j.getWidth(),this.j.getHeight());
this.j.removeAll();
this.j.add(imagePanel);
} else {
imagePanel.set(image);
}
repaint();
}
public RenderedOp LoadTiff(String nom) {
float ratio=0;
float ratioDecalage=0.01f;
float ratioLargeur=0;
float ratioHauteur=0;
String image =nom;
RenderedOp im = JAI.create("fileload", image);
int hauteur=im.getHeight();
int largeur=im.getWidth();
ratioLargeur=(float)this.j.getWidth()/largeur;
ratioHauteur=(float)this.j.getHeight()/hauteur;
if (ratioLargeur>=ratioHauteur) {
ratio=ratioHauteur;
} else {
ratio=ratioLargeur;
}
ratio=ratio-ratioDecalage;
Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_NEAREST);
ParameterBlock pb = new ParameterBlock();
pb.addSource(im); // The source image
pb.add(ratio); // The x origin
pb.add(ratio); // The y origin
pb.add(ratio); // The x translation
pb.add(ratio); // The y translation
pb.add(interp); // The interpolation
im = JAI.create("scale",pb,null);
return im;
}
/**Permet de rafraîchir le component*/
public void paint(Graphics g) {
super.paint(g);
if(thumbnail != null) {
int x = getWidth()/2 - thumbnail.getIconWidth()/2;
int y = getHeight()/2 - thumbnail.getIconHeight()/2;
if(y < 0) {
y = 0;
}
if(x < 5) {
x = 5;
}
thumbnail.paintIcon(this, g, x, y);
} |
Pour ce qui est du resultat,une fenetre vide s'ouvre puis quelques temps plus tard le jfilechooser,des que je clique sur une image,le jfilechooser se ferme pour faire apparaitre le jpanel avec l'image selectionné
Ce que je desirerais c(est le jfilechooser en haut,le jpanel en bas,des qui selectionne l image(non en faisant le bouton ouvirir),l image apparait en bas dans le jpanel,j avais reussi mais le prbl etait impossible de cliquer ensuite sur le bouton ouvir
Merci de votre aide