Bouton parcourir et ajout image jpanel

cs_aurel_kb Messages postés 17 Date d'inscription samedi 25 novembre 2006 Statut Membre Dernière intervention 27 décembre 2012 - 17 déc. 2012 à 23:00
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 18 déc. 2012 à 10:49
Bonjour,
j'ai un bouton parcourir sur ma fenêtre java, ainsi qu'un Jpanel afin d'aficher l'image obtenu grace au bouton. j'affiche la boite de dialogue de parcour, recupere bien le chemin du fichier image, mais j'arrive pas à l'afficher sur mon panel.

btnParcourir = new JButton("Parcourir...");

btnParcourir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

//Create a file chooser
final JFileChooser fc = new JFileChooser();
//int returnVal = fc.showOpenDialog(btnParcourir);
int returnVal = fc.showDialog(btnParcourir,"Attach");

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
String chemin = fc.getSelectedFile().getAbsolutePath().toString();
System.out.println(chemin);

try {
img = ImageIO.read(new File(chemin));
panImg.add(img);

} catch (IOException e) {
e.printStackTrace();
}
//log.append("Opening: " + file.getName() + "." + newline);

} else {
//log.append("Open command cancelled by user." + newline);
}

}
});

Aussi comment sauvegarde cette image si je clique sur un autre bouton Save par exemple.
Merci pour votre aide.
;) ;)


DE-AUREL

4 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
18 déc. 2012 à 08:17
0
cs_aurel_kb Messages postés 17 Date d'inscription samedi 25 novembre 2006 Statut Membre Dernière intervention 27 décembre 2012
18 déc. 2012 à 10:33
Comment adapter tout cella a mon code car j'ai essayé mais sans succès.

Code classe principal

import java.awt.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;



public class fich implements ActionListener{

private JFrame frame;
private JPanel pan;
private JTextField TxtRef;
private JTextField TxtNom;
private JTextField TxtPrix;
private JButton btnAnnuler;
private JButton btnValider;
private JButton btnSupprimer;
private JButton btnParcourir;
private JTextArea TxtDesc;
private String pathImage;
private Image img;
private JPanel panImg;

//JTextArea log;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
fich window = new fich();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public fich() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
//@SuppressWarnings("deprecation")
private void initialize() {
frame = new JFrame();
//Définit un titre pour notre fenêtre
frame.setTitle("Ajout d'un bien immobilier");
//Définit sa taille : 400 pixels de large et 100 pixels de haut
frame.setBounds(100, 100, 553, 350);
//Nous demandons maintenant à notre objet de se positionner au centre
frame.setLocationRelativeTo(null);
//Termine le processus lorsqu'on clique sur la croix rouge
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Garder la fenêtre en premier plan
frame.setAlwaysOnTop(true);
//Ne pas redimentionner la fenêtre
frame.setResizable(false);


//design du panel pan global
pan = new JPanel();
pan.setBackground(Color.GRAY);
pan.setLayout(null);

//design du panel top
JPanel ptop = new JPanel();
//ptop.setBackground(Color.LIGHT_GRAY);
ptop.setBounds(5, 6, 539, 262);
pan.add(ptop);
ptop.setLayout(null);

//Création des champs du formulaire
JLabel lblRef = new JLabel("Référence :");
lblRef.setBounds(8, 17, 75, 14);
ptop.add(lblRef);

JLabel lblNom = new JLabel("Nom :");
lblNom.setBounds(9, 47, 46, 14);
ptop.add(lblNom);

JLabel lblDescription = new JLabel("Description :");
lblDescription.setBounds(8, 120, 75, 14);
ptop.add(lblDescription);

TxtRef = new JTextField();
TxtRef.setBounds(170, 11, 86, 20);
ptop.add(TxtRef);

TxtNom = new JTextField();
TxtNom.setBounds(170, 41, 123, 20);
ptop.add(TxtNom);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(8, 145, 285, 102);
ptop.add(scrollPane);

TxtDesc = new JTextArea();
scrollPane.setViewportView(TxtDesc);

JLabel lblAjouterSiPossible = new JLabel("Ajouter si possible une image");
lblAjouterSiPossible.setBounds(334, 34, 195, 14);
ptop.add(lblAjouterSiPossible);

panImg = new JPanel();
//panImg.setBackground(Color.LIGHT_GRAY);
panImg.setBounds(324, 53, 205, 162);
ptop.add(panImg);

btnParcourir = new JButton("Parcourir...");

btnParcourir.addActionListener(this);
btnParcourir.setBounds(324, 224, 106, 23);
ptop.add(btnParcourir);

btnSupprimer = new JButton("Supprimer");
btnSupprimer.setBounds(433, 224, 96, 23);
ptop.add(btnSupprimer);

TxtPrix = new JTextField();
TxtPrix.setBounds(169, 72, 123, 20);
ptop.add(TxtPrix);
TxtPrix.setColumns(10);

JLabel lblPrixDuBien = new JLabel("Prix du bien :");
lblPrixDuBien.setBounds(8, 78, 120, 14);
ptop.add(lblPrixDuBien);

//design du panel droit pour les boutons
JPanel pbottom = new JPanel();
pbottom.setBounds(5, 270, 539, 43);
pan.add(pbottom);
pbottom.setLayout(null);

btnAnnuler = new JButton("Annuler");
//Fermeture de la fenêtre
btnAnnuler.addActionListener(this);
btnAnnuler.setBounds(268, 11, 89, 23);
pbottom.add(btnAnnuler);

btnValider = new JButton("Valider");
btnValider.addActionListener(this);
btnValider.setBounds(169, 11, 89, 23);
pbottom.add(btnValider);


//Et enfin ajout du panel pan,et rendre visible la frame
frame.setContentPane(pan);
//frame.show();
frame.setVisible(true);
}

//Parcours du bouton pour ajouter une image
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()== btnParcourir){

//Create a file chooser
final JFileChooser fc = new JFileChooser();
//int returnVal = fc.showOpenDialog(btnParcourir);


int returnVal = fc.showDialog(btnParcourir,"Attach");

if (returnVal == JFileChooser.APPROVE_OPTION) {
//File file = fc.getSelectedFile();
//This is where a real application would open the file.
String chemin = fc.getSelectedFile().getAbsolutePath().toString();
System.out.println(chemin);

try {

img = ImageIO.read(new File(chemin));
//img = ImageIO.read(new File("D:/COURs/932-1079-1082529-a.jpg"));
panImg.add(new ImageComponent(img));
//ImagePanel panel = new ImagePanel(new ImageIcon("D:/COURs/932-1079-1082529-a.jpg").getImage());
//panImg.add(panel);
panImg.repaint();

} catch (IOException ex) {
ex.printStackTrace();
}



System.out.println("Chargement ok");

} else {
System.out.println("impossible de charger l'image");
}

}
}

/*public void paintComponent(Graphics g){
if(img!=null){
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(img, 0, 0, panImg.getWidth(), panImg.getHeight(), null);
}
} */

}

Classe ImageComponent si c'est elle que j'utilise

public class ImageComponent extends Component {

private static final long serialVersionUID = 1;

private final java.awt.Image img;

public ImageComponent(java.awt.Image image)
{
img = image;
}

@Override
public void paint(java.awt.Graphics g)
{
g.drawImage(img, 0, 0, getWidth(), getHeight(), null, null);
}

}

classe ImagePanel si c'est elle que j'utilise

public class ImagePanel extends Component {

private static final long serialVersionUID = 1;

private final BufferedImage img;

ImagePanel(String fileName) throws IOException
{
img = ImageIO.read(new File(fileName));
}

public void paintComponent(Graphics g)
{
//super.paintComponent(g);
g.drawImage(img,0,0,null);
}

}

J'ai testé avec les deux mais rien ne s'affiche dans le panel. Je ne sais pas si c'est probleme avec le chemin de l'image, ou j'appel mal les classe. Ou est ce qu'on utilise vraiment la procedure PaintComponent?
Merci!!

DE-AUREL
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
18 déc. 2012 à 10:48
Tu ne l'utilises jamais, elle est appelée automatiquement lors de la construction de la fenêtre.
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
18 déc. 2012 à 10:49
Et essayes d'utiliser mon code, il fonctionne, n'adapte rien, récupères la classe et utilises là.
0
Rejoignez-nous