Bonjour je voudrais mettre en fond d'ecran d'une jframe une image qui se trouve sur mon disque dur. Je ne trouve pas l'erreur sur mon code pourtant j'ai essayé pas mal de chose, voilà mon code si quelqu'un a une idée merci de partager...

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
78
79
80
81
82
83
 
import javax.imageio.ImageIO;
import javax.swing.*;
 
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.*;
 
 
 
public class Fenetre extends JFrame{
 
public static void main(String[] args) throws IOException {
System.out.println("demarrage");
new Fenetre();
System.out.println("FIN");
 
}
 
 
public Fenetre() throws IOException{
super();
Image fond;
 
 
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension tailleEcran = tk.getScreenSize();
int largeurEcran = tailleEcran.width;
int hauteurEcran = tailleEcran.height;
 
fond = tk.getImage("src/Collines.jpg");
this.setIconImage(fond);
 
 
this.setTitle("Petite fenetre");
this.setSize(900, 700);
this.setLocation(largeurEcran*2/8, hauteurEcran*2/8);
 
 
FenetreFond fenFond = new FenetreFond("src/Collines.jpg");
try
{
//Image im = new ImageIcon("src/Collines.jpg").getImage();
//BufferedImage bimag = new BufferedImage(im.getWidth(null), im.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
//Image image = ImageIO.read(new File("src/Collines.jpg"));
Graphics g = fenFond.imag.getGraphics();
//g.drawImage(im,0,0,null);
fenFond.paintComponents(g);
//this.add(fenFond);
this.setContentPane(fenFond);
//this.getContentPane().setLayout(new BorderLayout());
this.pack();
this.setBounds(100,80,900,700);
this.setVisible(true);
System.out.println("on é la");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch(Exception e)
{
System.out.println("ERREUR");
e.printStackTrace();
}
System.out.println("on é sortie");
 
}
 
class FenetreFond extends JPanel
{
Image imag;
public FenetreFond(String s) throws IOException{
imag = ImageIO.read(new File(s));
//imag = new ImageIcon(s).getImage();
//imag = getToolkit().getImage(s);
}
public void paintComponents(Graphics g)
{
System.out.println("passons par la");
//Image imag = new ImageIcon("src/Collines.jpg").getImage();
g.drawImage(imag,0,0,getWidth(), getHeight(),this);
}
}
}