1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.awt.*;
import javax.swing.*;
public class Picture extends JPanel {
private Image img;
public Picture(String file) {
img = new ImageIcon(getClass().getResource(file)).getImage();
repaint();
}
@Override
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);
}
} |
Partager