Images can be drawn onto a JComponent
using the drawImage
method of class 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);
}
The x
and y
specify the location of the top-left of the image.