bonjour
juste pour savoir comment insérer une image d'arriere-plan(image de fond) sur un panel en java.
Merci![]()
bonjour
juste pour savoir comment insérer une image d'arriere-plan(image de fond) sur un panel en java.
Merci![]()
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(........); }
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![]()
voici le code pour une fenetre avec un panel dont l'image de fond est le logo de développez.com
si tu veux tester, prend l'image du site, tu l'enregistres dans un dossier images dans ton projet.
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); } }
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
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?![]()
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!
Partager