Background dans java

Résolu/Fermé
leen.net Messages postés 212 Date d'inscription lundi 31 janvier 2011 Statut Membre Dernière intervention 14 mai 2014 - 10 mars 2012 à 20:37
leen.net Messages postés 212 Date d'inscription lundi 31 janvier 2011 Statut Membre Dernière intervention 14 mai 2014 - 26 mars 2012 à 01:07
Bonjour,
comment je peux mettre un background dans java pour JPanel. non une couleur, mais plutôt une image



A voir également:

3 réponses

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
25 mars 2012 à 23:32
Un exemple :

class ImagePanel extends JPanel
{
	private final BufferedImage img;
	
	public ImagePanel(String filename) throws IOException
	{
		img = ImageIO.read(new File(filename));
		setPreferredSize(new Dimension(img.getWidth(),img.getHeight()));
	}
	
	@Override
	public void paint(Graphics g)
	{
		g.drawImage(img, 0, 0, null);
	};
}

class ImageFrame extends JFrame
{
	public ImageFrame(String filename) throws IOException 
	{
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(500,500);
		add(new ImagePanel(filename));		
	}
}

public class Test
{
	public static void main(String...args) throws IOException
	{
		new ImageFrame("D:\\test.png").setVisible(true);
	}
}
1