So I am using Eclipse with Windows builder. I was just wondering if there was anyway I can import an image that'll show up on the JFrame that I can easily move around and re-size instead of setting the location and size and drawing it.
-
5Add the image to a `JLabel` – Reimeus Aug 02 '13 at 23:58
-
That was a good idea, thanks for that. – user2612619 Aug 03 '13 at 00:05
4 Answers
There is no specialized image component provided in Swing (which is sad in my opinion). So, there are a few options:
- As @Reimeus said: Use a JLabel with an icon.
Create in the window builder a JPanel, that will represent the location of the image. Then add your own custom image component to the JPanel using a few lines of code you will never have to change. They should look like this:
JImageComponent ic = new JImageComponent(myImageGoesHere); imagePanel.add(ic);
where JImageComponent is a self created class that extends
JComponent
that overrides thepaintComponent()
method to draw the image.

- 67,591
- 47
- 198
- 287
If you are using Netbeans to develop, use JLabel and change its icon property.

- 4,115
- 6
- 33
- 42

- 1,365
- 20
- 27
-
2
-
Why only netbeans? Java is Java, regardless of whether you use netbeans, eclipse or anything else. – Andrea Oggioni Mar 08 '21 at 13:31
-
@AndreaOggioni Did I mentioned that only Netbeans? There are several ways to develop Java app. Using Netbeans is just one of them. My answer is for those who are using Netbeans. But it can applied other IDEs also. – rishad2m8 Mar 08 '21 at 20:47
As martijn-courteaux said, create a custom component it's the better option. In C# exists a component called PictureBox and I tried to create this component for Java, here is the code:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
public class JPictureBox extends JComponent {
private Icon icon = null;
private final Dimension dimension = new Dimension(100, 100);
private Image image = null;
private ImageIcon ii = null;
private SizeMode sizeMode = SizeMode.STRETCH;
private int newHeight, newWidth, originalHeight, originalWidth;
public JPictureBox() {
JPictureBox.this.setPreferredSize(dimension);
JPictureBox.this.setOpaque(false);
JPictureBox.this.setSizeMode(SizeMode.STRETCH);
}
@Override
public void paintComponent(Graphics g) {
if (ii != null) {
switch (getSizeMode()) {
case NORMAL:
g.drawImage(image, 0, 0, ii.getIconWidth(), ii.getIconHeight(), null);
break;
case ZOOM:
aspectRatio();
g.drawImage(image, 0, 0, newWidth, newHeight, null);
break;
case STRETCH:
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
break;
case CENTER:
g.drawImage(image, (int) (this.getWidth() / 2) - (int) (ii.getIconWidth() / 2), (int) (this.getHeight() / 2) - (int) (ii.getIconHeight() / 2), ii.getIconWidth(), ii.getIconHeight(), null);
break;
default:
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
}
}
}
public Icon getIcon() {
return icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
ii = (ImageIcon) icon;
image = ii.getImage();
originalHeight = ii.getIconHeight();
originalWidth = ii.getIconWidth();
}
public SizeMode getSizeMode() {
return sizeMode;
}
public void setSizeMode(SizeMode sizeMode) {
this.sizeMode = sizeMode;
}
public enum SizeMode {
NORMAL,
STRETCH,
CENTER,
ZOOM
}
private void aspectRatio() {
if (ii != null) {
newHeight = this.getHeight();
newWidth = (originalWidth * newHeight) / originalHeight;
}
}
}
If you want to add an image, choose the JPictureBox, after that go to Properties and find "icon" property and select an image. If you want to change the sizeMode property then choose the JPictureBox, after that go to Properties and find "sizeMode" property, you can choose some values:
- NORMAL value, the image is positioned in the upper-left corner of the JPictureBox.
- STRETCH value causes the image to stretch or shrink to fit the JPictureBox.
- ZOOM value causes the image to be stretched or shrunk to fit the JPictureBox; however, the aspect ratio in the original is maintained.
- CENTER value causes the image to be centered in the client area.
If you want to learn more about this topic, you can check this video.

- 47,830
- 31
- 106
- 135

- 155
- 2
- 4