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
| public class ImagePanel extends Panel {
private String image = "/resources/fond_ciws.jpg";
private final static int width = 240;
private final static int height = 320;
private BufferedImage bufferedImage;
public ImagePanel() {
URL url = getClass().getResource(image);
this.bufferedImage = this.toBufferedImage(Toolkit.getDefaultToolkit().getImage(url));
}
public void paint(Graphics g) {
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(), getHeight() );
g.drawImage(bufferedImage, 0, 0,null);
}
private BufferedImage toBufferedImage(Image image) {
image = new ImageIcon(image).getImage(); // SWING !!!
BufferedImage bufferedImage = new BufferedImage( width, height, 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;
}
} |
Partager