I am messing with java graphics for the first time, after a lot of searching I still can not seem to successfully add in a background, I Have my background image in a seperate file, pre-drawn and i'm trying to import it into my project.... Any help would be VERY appreciated!
public static void startMenu(){
ImagePanel panel = new ImagePanel(new ImageIcon("C:/Background.jpg").getImage());
// Loads the background image and stores in img object
Window game = new Window();
JFrame frame = new JFrame();
JButton startButton = new JButton("Start"); //makes a button with the text Start
startButton.setBounds(300, 400, 89, 23);
startButton.setVisible(true);
frame.getContentPane().add(startButton);
frame.getContentPane().add(panel);
frame.add(game);
frame.pack();
frame.setTitle(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null); // starts it in the center of the
// screen
frame.setVisible(true);
game.start(); // Initialize start method
}
public static void startButton(){
}
}
class ImagePanel extends JPanel {
private Image backgroundImg;
public ImagePanel(String backgroundImg) {
this(new ImageIcon(backgroundImg).getImage());
}
public ImagePanel(Image backgroundImg) {
Dimension size = new Dimension(backgroundImg.getWidth(null), backgroundImg.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(backgroundImg, 0, 0, null);
}
}