Problème chargement image lorsque l'on appuie sur un bouton

Fermé
bmartinkle Messages postés 4 Date d'inscription dimanche 17 mars 2013 Statut Membre Dernière intervention 18 mars 2013 - 17 mars 2013 à 15:46
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 - 17 mars 2013 à 18:47
Bonjour,
Je suis débutant en Java, faute de cours très médiocre dans mon école et on me demande de créer une application Java, donc j'ai choisi de créer un Paint mais j'ai beaucoup de problème pour mon codage des boutons.
Alors mon probleme est le suivant : j'aimerais lorsque j'appuie sur le bouton "Importer", que l'on puisse choisir une image dans nos fichier et que cette image s'importe dans le JPanel "mon Image"

Je vous mets mon code créé pour l'instant :

import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterJob;
import java.io.File;
import javax.swing.*;
import javax.swing.border.TitledBorder;


public class MaFenetreApplet extends JFrame {
private JPanel monCadre;
private JPanel monImage;
private JButton Enregistrer;
private JButton Arriere;
private JButton Avant;
private JButton RotationG;
private JButton RotationD;
private JButton Cercle;
private JButton Rectangle;
private JButton Gomme;
private JButton Pinceau;
private JButton Redimension;
private JComboBox Filtre;
private JSlider Intensite;
private JButton Importer;
private ImageIcon imageEnregistrer;
private ImageIcon imageArriere;
private ImageIcon imageAvant;
private ImageIcon imageRotG;
private ImageIcon imageRotD;
private ImageIcon imageCercle;
private ImageIcon imageRectangle;
private ImageIcon imageRedimension;
private ImageIcon imagePinceau;
private ImageIcon imageGomme;
private ImageIcon imageImporter;




public MaFenetreApplet() {

monCadre = new JPanel();
monImage = new JPanel();
monCadre.setLayout(null);
Importer = new JButton();
Enregistrer = new JButton();
Arriere = new JButton();
Avant = new JButton();
RotationG = new JButton();
RotationD = new JButton();
Cercle = new JButton();
Rectangle = new JButton();
Redimension = new JButton();
Pinceau = new JButton();
Gomme = new JButton();
Filtre = new JComboBox();
Intensite = new JSlider();


imageImporter = new ImageIcon("C://JDeveloper//mywork//Projet informatique/importer.png");
Importer.setIcon(imageImporter);
int X1 = imageImporter.getIconWidth();
int Y1 = imageImporter.getIconHeight();
Importer.setBounds (0,0,X1,Y1);

imageEnregistrer = new ImageIcon("C://JDeveloper//mywork//Projet informatique/enregistrer.png");
Enregistrer.setIcon(imageEnregistrer);
int X2 = imageEnregistrer.getIconWidth();
int Y2 = imageEnregistrer.getIconHeight();
Enregistrer.setBounds (X1,0,X2,Y2);

/* setBounds(X1+X2 normalement*/
imageArriere = new ImageIcon("C://JDeveloper//mywork//Projet informatique/arriere.png");
Arriere.setIcon(imageArriere);
int X3 = imageArriere.getIconWidth();
int Y3 = imageArriere.getIconHeight();
Arriere.setBounds (X1+X2,0,X3,Y3);

imageAvant = new ImageIcon("C://JDeveloper//mywork//Projet informatique/avant.png");
Avant.setIcon(imageAvant);
int X4 = imageAvant.getIconWidth();
int Y4 = imageAvant.getIconHeight();
Avant.setBounds (X1+X2+X3,0,X4,Y4);

imageRotG = new ImageIcon("C://JDeveloper//mywork//Projet informatique/rotg.png");
RotationG.setIcon(imageRotG);
int X5 = imageRotG.getIconWidth();
int Y5 = imageRotG.getIconHeight();
RotationG.setBounds (X1+X2+X3+X4,0,X5,Y5);

imageRotD = new ImageIcon("C://JDeveloper//mywork//Projet informatique/rotd.png");
RotationD.setIcon(imageRotD);
int X6 = imageRotD.getIconWidth();
int Y6 = imageRotD.getIconHeight();
RotationD.setBounds (X1+X2+X3+X4+X5,0,X6,Y6);

imageCercle = new ImageIcon("C://JDeveloper//mywork//Projet informatique/rond.png");
Cercle.setIcon(imageCercle);
int X7 = imageCercle.getIconWidth();
int Y7 = imageCercle.getIconHeight();
Cercle.setBounds (X1+X2+X3+X4+X5+X6,0,X7,Y7);

imageRectangle = new ImageIcon("C://JDeveloper//mywork//Projet informatique/rectangle.png");
Rectangle.setIcon(imageRectangle);
int X8 = imageRectangle.getIconWidth();
int Y8 = imageRectangle.getIconHeight();
Rectangle.setBounds (X1+X2+X3+X4+X5+X6+X7,0,X8,Y8);

imageRedimension = new ImageIcon("C://JDeveloper//mywork//Projet informatique/redimension.png");
Redimension.setIcon(imageRedimension);
int X9 = imageRedimension.getIconWidth();
int Y9 = imageRedimension.getIconHeight();
Redimension.setBounds (X1+X2+X3+X4+X5+X6+X7+X8,0,X9,Y9);

imagePinceau = new ImageIcon("C://JDeveloper//mywork//Projet informatique/pinceau.png");
Pinceau.setIcon(imagePinceau);
int X10 = imagePinceau.getIconWidth();
int Y10 = imagePinceau.getIconHeight();
Pinceau.setBounds (0,Y2,X10,Y10);

imageGomme = new ImageIcon("C://JDeveloper//mywork//Projet informatique/gomme.png");
Gomme.setIcon(imageGomme);
int X11 = imageGomme.getIconWidth();
int Y11 = imageGomme.getIconHeight();
Gomme.setBounds (0,Y2+Y10,X11,Y11);



Filtre.addItem("noir");
Filtre.addItem("rouge");
Filtre.addItem("vert");
Filtre.addItem("bleu");
Filtre.addItem("jaune");
Filtre.setBounds(X1+X2+X3+X4+X5+X6+X7+X8+X9,0,100,Y9);


monImage.setLocation(2*X11,2*Y2);


monCadre.add(this.Enregistrer);
monCadre.add(this.Arriere);
monCadre.add(this.Avant);
monCadre.add(this.RotationG);
monCadre.add(this.RotationD);
monCadre.add(this.Cercle);
monCadre.add(this.Rectangle);
monCadre.add(this.Redimension);
monCadre.add(this.Pinceau);
monCadre.add(this.Gomme);
monCadre.add(this.Importer);
monCadre.add(this.Filtre);
monCadre.add(this.monImage);
monCadre.add(this.Intensite);


Importer.addActionListener(new FenetreEvent());



this.setContentPane(monCadre);
this.setVisible(true);
this.setTitle("Traitement d'images");
this.setBounds(0,0,800,500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);





}

public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new MaFenetreApplet();
}
}



J'espère que vous allez pouvoir m'aider car je ne comprends vraiment pas comment utiliser JFileChooser et autre méthode qui me sont inconnus encore...

Merci pour toutes votre réponse, et excusez mon codage qui n'est pas très agréable a la lecture....

A voir également:

3 réponses

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
17 mars 2013 à 17:03
Ce serait bien de nous faire un zip avec toutes les images que tu appelles dans ton code, parce que sinon il est difficile de savoir ce que tu fais. (Tu peux mettre ton zip sur des sites comme https://www.petit-fichier.fr/)

De plus indique nous le code de la classe FenetreEvent, pour pouvoir tester ton code comme chez toi.
0
bmartinkle Messages postés 4 Date d'inscription dimanche 17 mars 2013 Statut Membre Dernière intervention 18 mars 2013
17 mars 2013 à 17:28
pour l'instant ma FenetreEvent est la suivante :


class FenetreEvent implements ActionListener {
    public void actionPerformed (ActionEvent e){
        if (e.getSource()== Importer) {
            Ouvrir ouv = new Ouvrir (monImage);
        }
    }
}



pour voir les images :
https://www.petit-fichier.fr/2013/03/17/arriere/
https://www.petit-fichier.fr/2013/03/17/avant/
https://www.petit-fichier.fr/2013/03/17/enregistrer/
https://www.petit-fichier.fr/2013/03/17/gomme/
https://www.petit-fichier.fr/2013/03/17/importer/
https://www.petit-fichier.fr/2013/03/17/pinceau/
https://www.petit-fichier.fr/2013/03/17/rectangle/
https://www.petit-fichier.fr/2013/03/17/redimension/
https://www.petit-fichier.fr/2013/03/17/rond/
https://www.petit-fichier.fr/2013/03/17/rotd/
https://www.petit-fichier.fr/2013/03/17/rotg/

Mais pour les images, ils sont importés depuis ma racine dans mon pc donc pas la meme que la votre...
0
KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
17 mars 2013 à 18:47
Voici comment tu peux modifier ton FenetreEvent pour utiliser un JFileChooser :

class FenetreEvent implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource() == Importer)
        {
            JFileChooser chooser = new JFileChooser();
            int etat = chooser.showOpenDialog(MaFenetreApplet.this);
            if (etat==JFileChooser.APPROVE_OPTION)
            {
                ImageIcon image = new ImageIcon(chooser.getSelectedFile().getAbsolutePath());
                ...
            }
        }
    }
}

Remarque : tu devrais regarder comment manipuler les Layout, parce que tu ne vas pas t'en sortir avec tous tes X et Y pour les setBounds : A Visual Guid to Layout Managers

Plus spécifiquement pour tes icônes, tu devrais aussi regarder comment faire un menu : How to Use Menus
0