hello a tous!

J'essaye d'afficher une image en fond de fenetre, bien disposer à l'aide d'un gridlayout sur un JPanel et je n'y arrive pas (et j'aimerai dailleur y faire figurer des icone dessus, mais plutard)...

Pourtant j'utilise le code qu'il y a ds la fac ...

voici mon code :

pr l'implementation de ce jcomponent:
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
 
import javax.swing.*; 
import java.awt.*; 
import java.awt.image.*; 
 
public class JPanelImageBg extends JComponent
{
	private int mode;
	private TexturePaint texture; 
	private BufferedImage bufferedImage; 
 
	public static final int CENTRE = 0;
	public static final int TEXTURE = 1;
 
	JPanelImageBg( String fileName, int mode )
	{	this.mode = mode;
		this.bufferedImage = this.toBufferedImage(Toolkit.getDefaultToolkit().getImage(fileName));
		this.texture = new TexturePaint(bufferedImage,new Rectangle(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight())); 
	} 
 
	public void paintComponent(Graphics g)
	{	switch( mode )
		{	case TEXTURE :
				Graphics2D g2d = (Graphics2D)g; 
				g2d.setPaint(texture);
				g2d.fillRect(0, 0, getWidth(), getHeight() );
				break;
			case CENTRE :
				g.setColor(this.getBackground());
				g.fillRect(0,0,getWidth(), getHeight() );
				g.drawImage(bufferedImage,(getWidth()-bufferedImage.getWidth())/2,(getHeight()-bufferedImage.getHeight())/2,null);
				break;
			default :
				super.paintComponents(g);
		}
	}
 
 
	private BufferedImage toBufferedImage(Image image)
	{	image = new ImageIcon(image).getImage(); 
 
		BufferedImage bufferedImage = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); 
		Graphics g = bufferedImage.createGraphics(); 
 
		g.setColor(Color.white); 
		g.fillRect(0, 0, image.getWidth(null), 
		image.getHeight(null)); 
		g.drawImage(image, 0, 0, null); 
		g.dispose(); 
		return bufferedImage; 
	}
 
}
et pr son utilisation :

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
 
import javax.swing.*;
 
public class JPanelImageBgTest extends JFrame
{
 
 
	JPanelImageBgTest(String titre, int mode )
	{	
		super(titre);
		this.setContentPane(new JPanelImageBg("butterfly.jpg",mode));
		this.getContentPane().add(new JButton("test"));
		setSize(500,500);
		this.setVisible(true);
	}
 
 
 
	public static void main( String[] argv )
	{	new JPanelImageBgTest("Centre",JPanelImageBg.CENTRE);
		new JPanelImageBgTest("Texture",JPanelImageBg.TEXTURE);
	}
voila lerreur :

Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(Unknown Source)
at agents.JPanelImageBg.toBufferedImage(JPanelImageBg.java:43)
at agents.JPanelImageBg.<init>(JPanelImageBg.java:18)
at agents.JPanelImageBgTest.<init>(JPanelImageBgTest.java:12)
at agents.JPanelImageBgTest.main(JPanelImageBgTest.java:21)


Merci de m'aider!!