Java
JPanel
Image Processing
Programming Tutorial
GUI Design

How to add an image to a JPanel in Java Swing

Codemia Knowledge Hub ·

Subclass JPanel and draw the image in paintComponent when the image is part of the panel itself, such as a background or something you scale. When it is just a static picture in the layout, add a JLabel holding an ImageIcon instead.

Drawing the image in paintComponent

java
1import java.awt.Dimension;
2import java.awt.Graphics;
3import java.awt.image.BufferedImage;
4import java.io.IOException;
5import javax.imageio.ImageIO;
6import javax.swing.JFrame;
7import javax.swing.JPanel;
8
9public class ImagePanel extends JPanel {
10    private final BufferedImage image;
11
12    public ImagePanel() throws IOException {
13        image = ImageIO.read(getClass().getResource("/images/logo.png"));
14        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
15    }
16
17    @Override
18    protected void paintComponent(Graphics g) {
19        super.paintComponent(g);
20        g.drawImage(image, 0, 0, this);
21    }
22
23    public static void main(String[] args) throws Exception {
24        JFrame frame = new JFrame("Image Panel");
25        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26        frame.add(new ImagePanel());
27        frame.pack();
28        frame.setLocationRelativeTo(null);
29        frame.setVisible(true);
30    }
31}

Three details matter:

  • call super.paintComponent(g) first
  • load the image once, not inside paintComponent
  • be careful with resource names and file paths

Loading the image in the paint method is a classic Swing performance mistake, because that method runs on every repaint.

Scaling the image to fit the panel

To make the image resize with the panel, draw it at the panel's current width and height:

java
1@Override
2protected void paintComponent(Graphics g) {
3    super.paintComponent(g);
4    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
5}

That stretches the image. To preserve the aspect ratio and center it:

java
1@Override
2protected void paintComponent(Graphics g) {
3    super.paintComponent(g);
4
5    int panelWidth = getWidth();
6    int panelHeight = getHeight();
7
8    double imageRatio = (double) image.getWidth() / image.getHeight();
9    double panelRatio = (double) panelWidth / panelHeight;
10
11    int drawWidth;
12    int drawHeight;
13
14    if (panelRatio > imageRatio) {
15        drawHeight = panelHeight;
16        drawWidth = (int) (drawHeight * imageRatio);
17    } else {
18        drawWidth = panelWidth;
19        drawHeight = (int) (drawWidth / imageRatio);
20    }
21
22    int x = (panelWidth - drawWidth) / 2;
23    int y = (panelHeight - drawHeight) / 2;
24
25    g.drawImage(image, x, y, drawWidth, drawHeight, this);
26}

Using a JLabel with an ImageIcon

If you do not need custom painting, a label is simpler:

java
1import javax.swing.ImageIcon;
2import javax.swing.JFrame;
3import javax.swing.JLabel;
4import javax.swing.JPanel;
5
6public class IconPanelExample {
7    public static void main(String[] args) {
8        JFrame frame = new JFrame("ImageIcon Example");
9        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10
11        JPanel panel = new JPanel();
12        panel.add(new JLabel(new ImageIcon(IconPanelExample.class.getResource("/images/logo.png"))));
13
14        frame.add(panel);
15        frame.pack();
16        frame.setLocationRelativeTo(null);
17        frame.setVisible(true);
18    }
19}

Here the image is just another child component, positioned by the layout manager like anything else.

Choosing between the two

Use custom painting when:

  • the image is part of the panel's visual surface
  • you need custom positioning or scaling
  • you want overlays, drawing or animation

Use JLabel plus ImageIcon when:

  • the image is static
  • layout managers should position it like any other component
  • you do not need custom painting logic

Loading the image from the classpath

Hard-coded file system paths make Swing apps brittle. Code that works in the IDE with new File("...") often breaks once the application is packaged. Load from the classpath instead:

java
BufferedImage image = ImageIO.read(getClass().getResource("/images/logo.png"));

This keeps working when the app is packaged as a JAR, as long as the image is bundled into the resources. If the resource might be missing, check for a null URL before reading so the failure is explicit and easy to diagnose.

Override paintComponent, not paint

A common mistake is overriding paint for normal component rendering. In Swing, paintComponent is the right hook for a panel's custom drawing.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design