Afficher une image après sélection JFileChooser
Bonjour à tous,
J'ai un problème d'affichage de fichier image dans un JPanel et de redimensionnement du JPanel . Effectivement je n'arrive pas à trouver l'erreur que je commets ou il me manque du code pour afficher une image dans le JPanel(pan). Je joints le
code afin de vous puissiez m'expliquer ce qui cloche dans ce programme.
Je vous remercie d'avance pour toutes les réponses et explications qui m'aiderons à évoluer.
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
package logiciel_fenetre;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Frame extends JFrame implements ActionListener {
private JPanel pan = new JPanel();
private JPanel jpl = new JPanel();
private JPanel cont = new JPanel();
private JButton btn1 = new JButton("Ouvrir");
private JButton btn2 = new JButton("Copier");
private Box panneauBouton = Box.createVerticalBox();
private JMenuBar menuBar = new JMenuBar();
private JMenu ficher = new JMenu("Ficher"),
aPropos = new JMenu("À propos");
private JMenuItem quitter = new JMenuItem("Quitter"),
aProposItem = new JMenuItem("Lisez-moi");
public Frame(){
//Création de la fenêtre.
this.setTitle("Logiciel");
this.setSize(800, 500);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//On rend la fenêtre visible.
this.setVisible(true);
cont.setLayout(new BorderLayout());
panneauBouton.setPreferredSize(new Dimension(360, 400));
btn1.setMaximumSize(new Dimension(200, 30));
btn1.setAlignmentX(JButton.CENTER_ALIGNMENT);
btn2.setMaximumSize(new Dimension(200, 30));
btn2.setAlignmentX(JButton.CENTER_ALIGNMENT);
jpl.setBounds(20, 20, 360, 400);
jpl.setBackground(Color.BLUE);
pan.setBackground(Color.GREEN);
pan.setBounds(380, 20, 360, 380);
cont.add(jpl);
cont.add(pan);
this.setContentPane(cont);
this.initMenu();
}
private void initMenu(){
//Menu ficher
ficher.add(quitter);
//Pour quitter l'application
quitter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
ficher.add(quitter);
//Menu À propos
aPropos.add(aProposItem);
//Ajout des menus dans la barre de menus
menuBar.add(ficher);
menuBar.add(aPropos);
//Ajout de la barre de menus sur la fenêtre
this.setJMenuBar(menuBar);
panneauBouton.add(Box.createVerticalStrut(150));
panneauBouton.add(btn1);
panneauBouton.add(Box.createVerticalStrut(50));
panneauBouton.add(btn2);
jpl.add(panneauBouton, BorderLayout.WEST);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()== btn1){
//Création d'un objet de type String.
@SuppressWarnings("unused")
String fichierImage;
//Création d'un composant JFileChooser.
JFileChooser dialogue = new JFileChooser();
//Ouvrir une boîte de dialogue qui permet
//la recherche d'un fichier.
int result = dialogue.showOpenDialog(Frame.this);
//Si un fichier a été sélectionné.
if(result == JFileChooser.APPROVE_OPTION) {
fichierImage = dialogue.getSelectedFile().getAbsolutePath();
}
}
@SuppressWarnings("unused")
class panneau extends JPanel {
private BufferedImage image;
public void paintComponent(Graphics g, String fichierImage){
super.paintComponent(g);
try {
BufferedImage image = ImageIO.read(new File(fichierImage));
ImageIcon icon = new ImageIcon(image);
JLabel lab = new JLabel(icon);
pan.setLayout(new BorderLayout());
pan.add(lab, BorderLayout.CENTER);
g.drawImage(image, 200, 200, this );
this.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
pan.setVisible(true);
repaint();
}
}
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Stub de la méthode généré automatiquement
}
} |