2

So i have created a program for the card game War. Everything is finished except I wanted to add a background. The problem is I don't know how to add a background picture so that the other JLabels and JButtons still appear at the top of the picture. Is there any for me set up a background picture so that the other panels show up on top as well? My program so far is this:

public War()
{
    JFrame frame = new JFrame();

    frame.setSize(500, 500);
    frame.setTitle("WAR");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    //Set up for background image
    Background = new JPanel();
    Background.add(new Background("Background.png"));
    frame.add(Background);
    Background.setLayout(new OverlayLayout(Background));

    cardPanel = new JPanel (new GridLayout(2,1));
    ///create labels and init with card back
    String strCard1 = "cardback.png";
    String strCard2 = "cardback.png";
    lblPlayer1Card = new JLabel(new ImageIcon(strCard1));
    lblPlayer2Card = new JLabel(new ImageIcon(strCard2));
    cardPanel.add(lblPlayer1Card);
    cardPanel.add(lblPlayer2Card);

    //Create panels for when war is played
    warPanel = new JPanel (new GridLayout(2, 3));
    String emptycards = "blankcard.png";
    player1war1 = new JLabel(new ImageIcon(emptycards));
    player1war2 = new JLabel(new ImageIcon(emptycards));
    player1war3 = new JLabel(new ImageIcon(emptycards));
    player2war1 = new JLabel(new ImageIcon(emptycards));
    player2war2 = new JLabel(new ImageIcon(emptycards));
    player2war3 = new JLabel(new ImageIcon(emptycards));
    warPanel.add(player1war1);
    warPanel.add(player1war2);
    warPanel.add(player1war3);
    warPanel.add(player2war1);
    warPanel.add(player2war2);
    warPanel.add(player2war3);


    // Create a panel
    btnPanel = new JPanel(new GridLayout(2,2));
    //set up next card button
    JButton btnNext = new JButton("Next Card");
    NextListener listener1 = new NextListener();
    btnNext.addActionListener(listener1);
    //set up new game button
    JButton btnNewGame = new JButton("New Game");
    NewGameListener listener2 = new NewGameListener();
    btnNewGame.addActionListener(listener2);


    //Sets up new panel showing size of main pile and discard pile of both players
    statusPanel = new JPanel(new GridLayout(4,2));

    //status labels that show the size of piles
    status1 = new JLabel("");// Size of player 1's main pile
    status2 = new JLabel("");//Size of player 1's discard pile
    status3 = new JLabel("");//Size of player 2's main pile
    status4 = new JLabel("");//Size of player 2's discard pile
    statusWar = new JLabel("");//Status label that shows the winner of War
    statusEmpty1 = new JLabel(""); // Creates empty status to put space between War status and size statuses
    statusEmpty2 = new JLabel(""); // Creates empty status to put space between War status and size statuses

    statusBar = new JLabel("");

    //Adds the labels to the panels
    btnPanel.add(btnNext);
    btnPanel.add(btnNewGame);
    btnPanel.add(statusBar);
    statusPanel.add(status1);
    statusPanel.add(status2);
    statusPanel.add(status3);
    statusPanel.add(status4);
    statusPanel.add(statusEmpty1);
    statusPanel.add(statusEmpty2);
    statusPanel.add(statusWar);

    cardPanel.setOpaque(false);
    btnPanel.setOpaque(false);
    statusPanel.setOpaque(false);
    warPanel.setOpaque(false);

    //The layout of the frame
    Background.add(cardPanel, BorderLayout.WEST);
    Background.add(btnPanel, BorderLayout.SOUTH);
    Background.add(statusPanel, BorderLayout.NORTH);
    Background.add(warPanel, BorderLayout.EAST);

    frame.setVisible(true);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

2

Create your self a JLabel. Load the image and apply it to the label (JLabel#setIcon).

Set the labels layout manager to what you need. Add your other components to the label as normal.

Add the label to the frame.

Take a look at

For examples

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Make "frame" an anonymous inner class. Override the "paint" method. Draw a background image via the Graphics object sent as a parameter in "paint". Then, call the super class's "paint", as to draw all the GUI elements. Example:

final Image backgroundImage = ...(load image);
JFrame frame = new JFrame() {
    @Override
    public void paint(java.awt.Graphics g) {
        g.drawImage(backgroundImage); // draw the background image
        super.paint(); // call the superclass method so all the 
                                // GUI elements are still drawn.
    }
};

Of course, you don't have to make it an anonymous inner class if you have some personal vendetta against them. You could always do this:

public class GameFrame extends JFrame {
    private Image backgroundImage;
    public GameFrame() {
        super(); // call the super constructor 
        backgroundImage = ...(load image);
    }
    @Override
    public void paint(java.awt.Graphics g) {
        g.drawImage(backgroundImage); // draw the background image
        super.paint(); // call the superclass method so all the 
                                // GUI elements are still drawn.
    }
}

EDIT: I'm looking at this, and you might have to do what I have shown here with the JPanel "Background" instead of the JFrame "frame", since it looks like the JPanel completely covers "frame". If doing what I have showed up here with the class inheriting JFrame does not work, I would assume it is because the JPanel is covering up anything drawn on the JFrame, so just do the same thing except with "Background".

Name
  • 2,037
  • 3
  • 19
  • 28
  • 1. `JFrame` does not have a `paintComponent` method. 2. Calling `super.paintComponent` will (generally) clear any previous changes made to the `Graphics` context - that's it's job... – MadProgrammer Dec 19 '12 at 01:08
  • @MadProgrammer Changed it, i think this will do. – Name Dec 19 '12 at 01:09
  • Sorry, I am fairly new to programming so I did not understand what you quite said. Could you rephrase that? – user1914437 Dec 19 '12 at 01:35
  • @user1914437 I guess my answer assumes you know about quite a bit of programming concepts. Look at MadProgrammer's answer, you'll understand it. – Name Dec 19 '12 at 16:04