How to center align background image in JPanel

Posted by Jaguar on Stack Overflow See other posts from Stack Overflow or by Jaguar
Published on 2010-12-26T10:43:30Z Indexed on 2010/12/26 10:54 UTC
Read the original article Hit count: 343

Filed under:
|
|
|

I wanted to add background image to my JFrame.
Background image means I can later add Components on the JFrame or JPanel
Although I coudn't find how to add background image to a JFrame,
I found out how to add background image to a JPanel from here:
How to set background image in Java?

This solved my problem, but now since my JFrame is resizable I want to keep the image in center.
The code I found uses this method

public void paintComponent(Graphics g) { //Draw the previously loaded image to Component.  
    g.drawImage(img, 0, 0, null);   //Draw image
}  

Can anyone say how to align the image to center of the JPanel.
As g.drawImage(img, 0, 0, null); provides x=0 and y=0
Also if there is a method to add background image to a JFrame then I would like to know.
Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about jframe