IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Insérer une image de fond en Java


Sujet :

AWT/Swing Java

  1. #1
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Mars 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2009
    Messages : 7
    Points : 4
    Points
    4
    Par défaut Insérer une image de fond en Java
    Salut à tous;
    Voilà, je suis débutant en Java. J'ai du insérer une image de fond. J'ai pu écrire un code mais le problème que au lieu l'image voulue s'affiche, un écran noir le fait avec plusieurs messages d'erreurs.
    Voilà mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class ImageFond extends JFrame {
            private Container c;
            private JPanel imagePanel;
            private String filePath;
     
            public ImageFond(String filePath) {
                    super("Image de fond");
                    this.filePath = filePath;
                    initialize();
            }
     
            private void initialize() {
                    setDefaultCloseOperation(EXIT_ON_CLOSE);
                    c = getContentPane();
                    imagePanel = new JPanel() {
                            public void paint(Graphics g) {
                                    try {
                                            BufferedImage image = ImageIO.read(new File(filePath));
                                            g.drawImage(image, 0, 0, null);
                                    } catch (IOException e) {
                                            e.printStackTrace();
                                    }
                            }
                    };
                    imagePanel.setPreferredSize(new Dimension(640, 480));
                    c.add(imagePanel);
            }
     
            public static void main(String[] args) {
                    String imagePath = "C:/fannoucha.JPEG";
                    ImageFond fond = new ImageFond(imagePath);
                    fond.pack();
                    fond.setVisible(true);
            }
    }
     
    Et voilà les messages d'erreurs:
     
    javax.imageio.IIOException: Can't read input file!
    	at javax.imageio.ImageIO.read(ImageIO.java:1263)
    	at ImageFond$1.paint(ImageFond.java:29)
    	at javax.swing.JComponent.paintChildren(JComponent.java:647)
    	at javax.swing.JComponent.paint(JComponent.java:817)
    	at javax.swing.JComponent.paintChildren(JComponent.java:647)
    	at javax.swing.JComponent.paint(JComponent.java:817)
    	at javax.swing.JLayeredPane.paint(JLayeredPane.java:552)
    	at javax.swing.JComponent.paintChildren(JComponent.java:647)
    	at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4778)
    	at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4724)
    	at javax.swing.JComponent.paint(JComponent.java:798)
    	at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
    	at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
    	at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
    	at java.awt.Container.paint(Container.java:1309)
    	at sun.awt.RepaintArea.paint(RepaintArea.java:177)
    	at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
    	at java.awt.Component.dispatchEventImpl(Component.java:3699)
    	at java.awt.Container.dispatchEventImpl(Container.java:1623)
    	at java.awt.Window.dispatchEventImpl(Window.java:1590)
    	at java.awt.Component.dispatchEvent(Component.java:3480)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
    J'espère bien que vous m'aideriez.

    Merci

  2. #2
    Membre habitué Avatar de erictoguem
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2006
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2006
    Messages : 150
    Points : 187
    Points
    187
    Par défaut
    est ce que tu as bien précisé le chemin de ton image en tennant comte de la casse des caractères?

  3. #3
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Mars 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2009
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    RE;
    Oui, je suis sur du chemin d'accès

  4. #4
    Rédacteur
    Avatar de darrylsite
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 299
    Points : 2 501
    Points
    2 501
    Par défaut
    le chemin de l'image n'est pas correcte. Sous windows, le separateur est '\' et non '/'. Le chemin correct est :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     String imagePath = "C:\\fannoucha.JPEG";

  5. #5
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Mars 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2009
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    J'ai bien corrigé le chemin d'accès, mais le problème reste le meme. Un écran noir s'affiche au lieu de l'image.

  6. #6
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Citation Envoyé par darrylsite Voir le message
    le chemin de l'image n'est pas correcte. Sous windows, le separateur est '\' et non '/'. Le chemin correct est :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     String imagePath = "C:\\fannoucha.JPEG";

    En java on peut utiliser indifféremment / ou \\ comme séparateur dans les path windows. Pour moi le problème viendrait plutôt de la casse(minuscule/majuscule) du nom du fichier.

  7. #7
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Mars 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2009
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    J'ai bien respecté la casse. Vraiment je sais pas d'où vient le problème?

  8. #8
    Rédacteur
    Avatar de darrylsite
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 299
    Points : 2 501
    Points
    2 501
    Par défaut
    Citation Envoyé par sinok Voir le message
    En java on peut utiliser indifféremment / ou \\ comme séparateur dans les path windows. Pour moi le problème viendrait plutôt de la casse(minuscule/majuscule) du nom du fichier.
    Je ne crois que ça soit correcte. Pourquoi utilise-t-on alors le pathSeparator?

    J'ai bien corrigé le chemin d'accès, mais le problème reste le meme. Un écran noir s'affiche au lieu de l'image.
    Les exceptions sont elles toujours presentes?

  9. #9
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Mars 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2009
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    Mouaip

  10. #10
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Citation Envoyé par darrylsite Voir le message
    Je ne crois que ça soit correcte. Pourquoi utilise-t-on alors le pathSeparator?
    Le path separator sert dans une obtique de présentation essentiellement, essaies avec n'importe quel fichier, tu verras que ça marche.

  11. #11
    Rédacteur
    Avatar de darrylsite
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 299
    Points : 2 501
    Points
    2 501
    Par défaut
    je viens d'essayer et ça marche.
    Le code fourni marche bien aussi. Je crois que le probleme vient de la casse, ou de l'extension du fichier ".jpg" au lieu de ".JPEG". Chez moi, ça marche bien.

  12. #12
    Membre actif Avatar de jiddou
    Inscrit en
    Août 2007
    Messages
    247
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 247
    Points : 251
    Points
    251
    Par défaut
    Il n'y a aucun problème. Dans ton code.Preuve: je l'ai testé.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class ImageFond extends JFrame {
            private Container c;
            private JPanel imagePanel;
            private String filePath;
     
            public ImageFond(String filePath) {
                    super("Image de fond");
                    this.filePath = filePath;
                    initialize();
            }
     
            private void initialize() {
                    setDefaultCloseOperation(EXIT_ON_CLOSE);
                    c = getContentPane();
                    imagePanel = new JPanel() {
                            public void paint(Graphics g) {
                                    try {
                                            BufferedImage image = ImageIO.read(new File(filePath));
                                            g.drawImage(image, 0, 0, null);
                                    } catch (IOException e) {
                                            e.printStackTrace();
                                    }
                            }
                    };
                    imagePanel.setPreferredSize(new Dimension(640, 480));
                    c.add(imagePanel);
            }
     
            public static void main(String[] args) {
                    String imagePath = "C:/A.jpg";
                    ImageFond fond = new ImageFond(imagePath);
                    fond.pack();
                    fond.setVisible(true);
            }
    }
    Donc c'est effectivement un problème de chemin mais pas des slashs.
    Assures toi que c'est ça l'extention(JPEG) (La casse importe peu sous windows,mais sous linux garre à toi).

    Encore un autre conseil si t'es sous windows assures toi que ton système affiche les extensions(ne le fait pas par défaut) parce que sinon en ajoutant les extensions tu peux facilement te retrouver avec test.JPEG.jpg

  13. #13
    Candidat au Club
    Profil pro
    Étudiant
    Inscrit en
    Mars 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2009
    Messages : 7
    Points : 4
    Points
    4
    Par défaut
    Salut à tous;
    Voilà, mon problème est résolu. C'est vrai mon système n'affiche pas les extensions. De plus, j'ai du changer l'extension .JPEG en jpg et ça marchait bien!
    Merci infiniment à vous tous!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Insérer une image en fond d'un JTextField
    Par isa911Bis dans le forum Composants
    Réponses: 2
    Dernier message: 19/12/2011, 09h57
  2. Insérer une image en fond
    Par najma dans le forum Interfaces Graphiques
    Réponses: 10
    Dernier message: 02/01/2010, 02h23
  3. [Toutes versions] Insérer une image de fond qui ne s'imprimera pas
    Par Sembey dans le forum Word
    Réponses: 2
    Dernier message: 28/03/2009, 01h51
  4. Insérer une image de fond sous un graphique
    Par sango85 dans le forum MATLAB
    Réponses: 5
    Dernier message: 05/03/2007, 18h55
  5. Réponses: 5
    Dernier message: 27/09/2005, 10h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo