bonjour
juste pour savoir comment insérer une image d'arriere-plan(image de fond) sur un panel en java.
Merci:)
Version imprimable
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:
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 :cry:
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:
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?8O
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!
Oui j'ai fait exactement ce que tu m'as dit mais ca ne marche tjrs pas.
tu as utilisé exactement le méme code?
envoie moi ton projet par mail que je teste
michel.59000@gmail.com
Ok c'est fait,je te l'ai envoyé,le nom du projet est "Test".
Merci pour ton aide:ccool:
j'ai supprimé settings et bon des sources et ça marche! :)
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.
Donc on fait quoi finalement?tu me redonnes le code que t'as modifié?
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).
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 :ccool: