I have this code and I'm trying to add a picture display as a wallpaper in the background. I used this stackoverflow post as a reference but it didn't work. My image is being added to the panel although not as a background. What possibly is the problem?
Side note, .setLocation()
isn't updating the location of buttons either. Thanks in advance!
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.*;
class APP extends JPanel
{
JFrame frame = new JFrame();
JPanel panel;
class ImagePanel extends JComponent {
Image image;
public ImagePanel(Image image) {
this.image = image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 100, 25, this);
}
}
public APP(){
JLabel picLabel = new JLabel();
try {
BufferedImage image = ImageIO.read(getClass().getResource("login_bg.jpg"));
this.setContentPane(new ImagePanel(image));
picLabel = new JLabel(new ImageIcon(image));
//picLabel.setBounds(100, 25, 843, 568);
} catch (IOException e){
e.printStackTrace();
}
panel = new JPanel(); // used to do padding like in HTML
panel.setBorder(BorderFactory.createEmptyBorder(50, 350, 300, 350));
panel.setLayout(null));
panel.add(picLabel);
frame.add(panel, BorderLayout.CENTER);
frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Drone Delivery");
frame.pack();
frame.setVisible(true);
}
}