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
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:
That stretches the image. To preserve the aspect ratio and center it:
Using a JLabel with an ImageIcon
If you do not need custom painting, a label is simpler:
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:
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.
Related reading
- How to automatically detect and crop individual sprite bounds in sprite sheet?
- How to build an image classification dataset in Azure?
- How to calculate the output size after convolving and pooling to the input image
- How to cartoon-ify an image programmatically?
- How to add Cache-Control header to static resource in Spring Boot?
- How to add context.xml file to embedded tomcat server
- How to categorize True Negatives in sliding window object detection?
- how to check both training/eval performances in tensorflow object_detection
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.