burntsugar
burntsugar

Reputation: 58030

Display a jpg image on a JPanel

What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel?

Cheers.

Upvotes: 16

Views: 119719

Answers (5)

Handsken
Handsken

Reputation: 674

You could also use

ImageIcon background = new ImageIcon("Background/background.png");
JLabel label = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(background);

JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(label);

if your working with a absolut value as layout.

Upvotes: 1

Per Alexandersson
Per Alexandersson

Reputation: 2523

I would use a Canvas that I add to the JPanel, and draw the image on the Canvas. But Canvas is a quite heavy object, sine it is from awt.

Upvotes: 1

Eugene Ryzhikov
Eugene Ryzhikov

Reputation: 17359

ImageIcon image = new ImageIcon("image/pic1.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );

Upvotes: 28

Tom
Tom

Reputation: 44821

I'd probably use an ImageIcon and set it on a JLabel which I'd add to the JPanel.

Here's Sun's docs on the subject matter.

Upvotes: 2

Sean A.O. Harney
Sean A.O. Harney

Reputation: 24497

You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel.

Upvotes: 7

Related Questions