Show image in Java JFrame

What is the best way to display an image at specific coordinates in java JFrame?

I know that there are several ways to do this, I just need to know the best way to display the image that I plan to move around the frame!

+7
source share
3 answers

I would suggest adding it as ImageIcon:

ImageIcon image = new ImageIcon("image.jpeg"); add(image); 

EDIT (how to add it add specific coordinates):

At first, a note, usually (99% of cases), is not recommended to host JComponents yourself. Use the LayoutManager to arrange them for you and handle resizing.
I will tell you how to position it correctly:

  • Set LayoutManager to null : setLayout(null) .
  • set ImageIcon to specific coordinates: image.setLocation(x, y) .
+3
source

Using ImageIcon with JLabel is the easiest way. In fact, you can add this to the level in the JFrame JLayeredPane, which is above or below the content area depending on your requirements.

+3
source

Design a custom component and override the paintComponent() method. Developing a custom component gives you the flexibility to make further improvements. This way you can easily add panning, zooming, etc.

Then just add this component to your JFrame .

+2
source

All Articles