public void paint(Graphics g){
super.paint(g);
g.drawImage(imagenFondo, 0, 0, getWidth(), getHeight(), this);
}
Don't override paint(). The paint method is responsible for painting the child components. So your code paints the child components and then draws the image over top of the components.
Instead, for custom painting of a component you override the paintComponent()
method of a JPanel
:
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imagenFondo, 0, 0, getWidth(), getHeight(), this);
}
Read the section from the Swing tutorail on A Closer Look at the Paint Mechanism for more information.
Edit:
Read the entire section from the Swing tutorial on Custom Painting
. The solution is to do the custom painting on a JPanel and then add the panel to the frame.
The content pane of a frame is a JPanel. So you will in effect be replacing the default content pane with your custom JPanel that paints the background image. Set the layout of your custom panel to a BorderLayout
and it will work just like the default content pane.