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

 Java Discussion :

image d'arriere-plan sur un panel


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Juin 2005
    Messages
    578
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 578
    Par défaut image d'arriere-plan sur un panel
    bonjour
    juste pour savoir comment insérer une image d'arriere-plan(image de fond) sur un panel en java.
    Merci

  2. #2
    Membre émérite
    Avatar de michel.di
    Homme Profil pro
    Freelance
    Inscrit en
    Juin 2009
    Messages
    782
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 782
    Par défaut
    tu dois surcharger la méthode paint. Pour cela il faut que ta classe étende JPanel

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    public void paint(Graphics g){
    g.drawImage(........);
    }

  3. #3
    Membre éclairé
    Inscrit en
    Juin 2005
    Messages
    578
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 578
    Par défaut
    Ok je vois,mais le probléme c'est que je ne suis qu'un débutant en java,donc si tu pouvais m'expliquer pas à pas comment faire,tu serais vraiment le messi.
    En fait j'avais fais des recherches mais j'avais pas trop bien compris comment faire,et parfois l'explication était en anglais,donc

  4. #4
    Membre émérite
    Avatar de michel.di
    Homme Profil pro
    Freelance
    Inscrit en
    Juin 2009
    Messages
    782
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 782
    Par défaut
    voici le code pour une fenetre avec un panel dont l'image de fond est le logo de développez.com

    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
     
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    /**
     * 
     */
     
    /**
     * @author michel
     *
     */
    public class JPanelDeveloppez extends JPanel {
     
    	@Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		Image image = new ImageIcon(JPanelDeveloppez.class.getResource("/logo.png")).getImage();
    		g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
    	}
     
    	@Override
    	public Dimension getPreferredSize() {
    		return new Dimension(210,80);
    	}
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(new JPanelDeveloppez());
     
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    }
    si tu veux tester, prend l'image du site, tu l'enregistres dans un dossier images dans ton projet.
    Ce dossier images tu l'ajoutes aux sources de ton projet ( click droit sur le projet -> propriétés -> java buid path -> source -> add folder

    et voila ça doit marcher!
    si tu veux plus de précisions sur le code n'hésite pas

  5. #5
    Membre éclairé
    Inscrit en
    Juin 2005
    Messages
    578
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 578
    Par défaut
    Aye ça marche pas,il m'affiche l'erreur suivante:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at JPanelDeveloppez.paint(JPanelDeveloppez.java:23)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at JPanelDeveloppez.paint(JPanelDeveloppez.java:23)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    Apparemment il ne trouve pas l'image.Pourtant j'ai bien suivi tes instructions.Au fait votre logo a une extension ".gif".Donc au lieu de "/logo.png",j'ai mis "/logo.gif".J'espère que ton code prends en compte les extensions ".gif".
    Sinon j'ai même essayé avec extension ".png".Mais ça m'affiche tjrs la même erreur.
    Que faire maintenant?

  6. #6
    Membre émérite
    Avatar de michel.di
    Homme Profil pro
    Freelance
    Inscrit en
    Juin 2009
    Messages
    782
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 782
    Par défaut
    gif ou png ça marche normalement!
    tu as bien ajouté le dossier images dans lequel tu as placé le logo.gif ou png en sources du projet?
    avec tout ça ça doit fonctionner normalement!

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

Discussions similaires

  1. [WD15] Image en arriere plan sur un etat
    Par cheikhoufia dans le forum WinDev
    Réponses: 4
    Dernier message: 03/09/2012, 15h30
  2. image comme arrière-plan dans une fenetre?
    Par afrikha dans le forum GTK+ avec C & C++
    Réponses: 6
    Dernier message: 08/10/2005, 20h22
  3. Comment mettre une image en arriere plan
    Par huon dans le forum Access
    Réponses: 1
    Dernier message: 13/09/2005, 16h16
  4. Centrage de texte avec une image en arriere plan
    Par MasterOfChakhaL dans le forum Balisage (X)HTML et validation W3C
    Réponses: 7
    Dernier message: 26/08/2005, 18h31
  5. image d'arriere plan
    Par mikky dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 12/05/2005, 18h05

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