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:
J'espère bien que vous m'aideriez.
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)
Merci
Partager