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

  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!

  7. #7
    Membre éclairé
    Inscrit en
    Juin 2005
    Messages
    578
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 578
    Par défaut
    Oui j'ai fait exactement ce que tu m'as dit mais ca ne marche tjrs pas.

  8. #8
    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 as utilisé exactement le méme code?
    envoie moi ton projet par mail que je teste
    michel.59000@gmail.com

  9. #9
    Membre éclairé
    Inscrit en
    Juin 2005
    Messages
    578
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 578
    Par défaut
    Ok c'est fait,je te l'ai envoyé,le nom du projet est "Test".
    Merci pour ton aide

  10. #10
    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
    j'ai supprimé settings et bon des sources et ça marche!

  11. #11
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 913
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 913
    Billets dans le blog
    54
    Par défaut
    Il est plus recommande de surcharger paintComponent() que paint(), surtout si tu comptes rajouter d'autres composants dans ce panneau.

    Egalement comme tu peux le penser c'est une question qui revient souvent donc faudrait penser a faire des recherches sur le forum avant de la poster.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  12. #12
    Membre éclairé
    Inscrit en
    Juin 2005
    Messages
    578
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 578
    Par défaut
    Donc on fait quoi finalement?tu me redonnes le code que t'as modifié?

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

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Par défaut
    Bah tu vas dans les sources gentiment mises à disposition par le site et tu récupères l'exemple qui marche afin de le comprendre: http://java.developpez.com/sources/?...mageBackground

    Sachant que l'exemple fourni ci dessus possède quelques problèmes: redéfinition de paint alors qu'il faut redéfinir paintComponent, chargement de l'image dans la méthode paint (histoire bien bloquer l'interface à chaque repaint).

  14. #14
    Membre éclairé
    Inscrit en
    Juin 2005
    Messages
    578
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 578
    Par défaut
    Wouuuuuuuuh ça marche,mais je dois avouer que c'est pas évident de trouver ce qu'on cherche sur votre site,surtout pour un débutant,car y'a trop de FAQ,tutoriels,etc..par finir on sait plus par ou commencer,bon je pense que je m'habituerais avec le temps.
    Encore merci

+ 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, 16h30
  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, 21h22
  3. Comment mettre une image en arriere plan
    Par huon dans le forum Access
    Réponses: 1
    Dernier message: 13/09/2005, 17h16
  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, 19h31
  5. image d'arriere plan
    Par mikky dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 12/05/2005, 19h05

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