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
|
import java.awt.*;
import javax.swing.*;
public class fenetre extends JPanel {
private Image img;
public fenetre(String file) {
img = new ImageIcon(getClass().getResource(file)).getImage();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (img == null) return;
int w = img.getWidth(this);
int h = img.getHeight(this);
boolean zoom = (w > getWidth() || h > getHeight());
if (zoom) g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
else g.drawImage(img, (getWidth()-w)/2, (getHeight()-h)/2, this);
}
public static void main(String arg[]){
fenetre fen = new fenetre("russie.jpg");
fen.show();
}
} |
Partager