绘制图像
图像可以被绘制到一个 JComponent 使用类的 drawImage 方法 Graphics :
加载图像
BufferedImage img;
try {
img = ImageIO.read(new File("stackoverflow.jpg"));
} catch (IOException e) {
throw new RuntimeException("Could not load image", e);
}
绘制图像
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int y = 0;
g.drawImage(img, x, y, this);
}
x 和 y 指定图像左上角的位置。