I am trying to set a background image in Eclipse using Java and I think that I have most of it done.
I am trying to make a 2D game and I want to add a background image to my menuJFrame
which you will see below.
I have this code already:
This is my main class JFrames
:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JFrames extends JPanel implements ActionListener {
JFrame menuJFrame,howToPlayJFrame, level1JFrame;
JPanel menuJPanel,howToPlayJPanel;
JButton howToPlayButton,backToMainMenuButton,startGameButton, quitProgramButton;
JLabel howToPlayLabel;
public static void main(String [] args){
JFrames jframes = new JFrames();
}
public JFrames(){
//menuJFrame
menuJFrame = new JFrame("SquareRun/Menu");
menuJFrame.setVisible(true);
menuJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuJFrame.setSize(800, 600);
//menuJPanel
menuJPanel = new JPanel();
menuJFrame.add(menuJPanel);
howToPlayButton = new JButton("How To Play");
menuJPanel.add(howToPlayButton);
howToPlayButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == howToPlayButton){
howToPlayJFrame.setVisible(true);
}
}});
startGameButton = new JButton("Start Game");
menuJPanel.add(startGameButton);
startGameButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startGameButton)
level1JFrame.setVisible(true);
menuJFrame.setVisible(false);
}});
quitProgramButton = new JButton("Quit Game");
menuJPanel.add(quitProgramButton);
quitProgramButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == quitProgramButton){
menuJFrame.dispose();
}
}});
//howToPlayJFrame
howToPlayJFrame = new JFrame("SquareRun/HowToPlay");
howToPlayJFrame.setVisible(false);
howToPlayJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
howToPlayJFrame.setSize(600, 100);
//howToPlayJPanel
howToPlayJPanel = new JPanel();
howToPlayJFrame.add(howToPlayJPanel);
howToPlayLabel = new JLabel("Use the arrow keys to move, Up= jump, Down= down, Right= right, Left= left");
howToPlayJPanel.add(howToPlayLabel);
backToMainMenuButton = new JButton("Close Window");
howToPlayJPanel.add(backToMainMenuButton);
backToMainMenuButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == backToMainMenuButton){
howToPlayJFrame.setVisible(false);
howToPlayJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}});
//level1JFrame
level1JFrame = new JFrame("Level 1");
level1JFrame.setVisible(false);
level1JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
level1JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
public void actionPerformed(ActionEvent e) {
}
public Object getCurrentLevel() {
return null;
}
}
This is my background class:
import java.awt.Image;
import javax.swing.ImageIcon;
public class Background extends JFrames {
private JFrames game;
private Image image;
public Background(JFrames game){
this.game = game;
image = (new ImageIcon("Image001.png")).getImage();
}
}