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
|
package jeu;
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 java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* Created on Jul 19, 2005
*
*/
/**
* @author Fery.P
*
*/
public class ImageFond extends JFrame {
private Container c;
private JPanel imagePanel;
private String filePath;
protected JButton b1,b2;
public ImageFond(String filePath) {
super("Image de fond");
this.filePath = filePath;
initialize();
}
private void initialize() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
c = getContentPane();
b1=new JButton("test1");
b2=new JButton("test2");
imagePanel = new JPanel() {
public void paint(Graphics g) {
try {
BufferedImage image = ImageIO.read(new File(filePath));
g.drawImage(image, 0, 0, this);
} catch (IOException e) {
e.printStackTrace();
}
}
};
imagePanel.revalidate();
imagePanel.setPreferredSize(new Dimension(640, 480));
c.add(imagePanel);
imagePanel.add(b1);
imagePanel.add(b2);
}
public static void main(String[] args) {
ImageFond fond=null;
String imagePath = "C:/fond_gris.jpg";
fond = new ImageFond(imagePath.toString());
fond.pack();
fond.setVisible(true);
}
}; |
Partager