Reputation: 59
I'm working on a project to design a game and I was wondering if it's possible to add an image to the JPanel directly without adding it to jLabel and then add the label to the panel.
Upvotes: 0
Views: 165
Reputation: 9914
you can try like this:
class Sample extends JPanel {
BufferedImage image;
Pseudo(BufferedImage image) {
this.image = image;
// or load it in this class
setLayout(null);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x =
int y =
g.drawImage(image, x, y, this);
}
}
This will add image directly to JPanel.
Upvotes: 1