user1172575
user1172575

Reputation: 59

Adding Image to JPanel directly

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

Answers (1)

UVM
UVM

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

Related Questions