swing Graphics Drawing images


Example

Images can be drawn onto a JComponent using the drawImage method of class Graphics:

loading an image

BufferedImage img;
try {
    img = ImageIO.read(new File("stackoverflow.jpg"));
} catch (IOException e) {
    throw new RuntimeException("Could not load image", e);
}

drawing the image

@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.