0
public class Board extends JFrame
{
     public void bd()
{


    JFrame frame=new JFrame();
    JLabel background1 = new JLabel(new ImageIcon("background.png"));
    JLabel knight=new JLabel(new ImageIcon("knight.jpg"));
    frame.add(background1);
    frame.add(knight);
    frame.pack();
    frame.setResizable(false);     
    frame.setVisible(true); 





    }
}

I've been having some trouble with my code

when i add the knight image the background image will disappear and only the knight image appears. How would i make the images overlap or make the background image act like a background

thebigone
  • 61
  • 1
  • 9
  • Just to clarify that the knight image would be infront of the background1 image – thebigone Feb 29 '16 at 22:08
  • @Andrew Thompson Is it possible to open this question? I have some solution which might be helpful to what OP actually wants to do... – user3437460 Feb 29 '16 at 22:48
  • @user3437460 Given 2 good answers, 1 accepted, and the duplicate status, it might be best to post your answer in response to a new (more specific) question asked by you. (Just like the Q&A I asked/answered & used as the duplicate.) – Andrew Thompson Feb 29 '16 at 23:04
  • BTW - given `knight` vaguely implies chess, see also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). It creates the chessboard not with a 'background image' but by using components. – Andrew Thompson Feb 29 '16 at 23:06
  • @AndrewThompson Hmm, I will do it as what you suggested. I was only afraid some users might thought I am doing a stunt for gaining reputation. – user3437460 Feb 29 '16 at 23:08
  • @user3437460 Asking and answering as a 'canonical Q&A' is encouraged by the powers that be/overriding user opinion. OTOH it will always p*ss some people off. (shrugs) Screw those people. – Andrew Thompson Feb 29 '16 at 23:11
  • @thebigone I have posted a solution here which may help you. http://stackoverflow.com/questions/35711891/how-to-create-a-background-and-foreground-image-which-overlaps/35711894#35711894 – user3437460 Feb 29 '16 at 23:33
  • @AndrewThompson I've posted a Q&A solution, hopefully I don't p*ss some of the users here `(>.<)!` – user3437460 Feb 29 '16 at 23:36

2 Answers2

1

JFrame uses BorderLayout by default, which means only one image is managed at the default/CENTER position.

See How to Use Borders for more details.

The solution is actually difficult, because you need to be able to make guarantees about the size of the layout, the images and the board

You might be able to use a GridLayout or GridBagLayout, but you'd need to have "empty" filler components to allow the layout to correctly expand, which could be come troublesome

Maybe a better solution might be to use a custom painting approach, which give you control over where the images are placed.

Painting in AWT and Swing and Performing Custom Painting for more detals

The following example simply makes use of GridBagLayout, which allows you to overlay components (in a variety of different ways). This example makes the boardLabel the container for all the other pieces, but, because the way GridBagLayout works, this isn't an absolute requirement

Chess

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Chess {

    public static void main(String[] args) {
        new Chess();
    }

    public Chess() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            BufferedImage board = ImageIO.read(getClass().getResource("/board.png"));
            BufferedImage knight = ImageIO.read(getClass().getResource("/Knight.png"));
            setLayout(new BorderLayout());
            JLabel boardLabel = new JLabel(new ImageIcon(board));
            add(boardLabel);

            boardLabel.setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            for (int row = 0; row < 8; row++) {
                gbc.gridy = row;
                for (int col = 0; col < 8; col++) {
                    gbc.gridx = col;
                    boardLabel.add(filler(), gbc);
                }
            }

            JLabel knightLabel = new JLabel(new ImageIcon(knight));

            gbc.gridx = 3;
            gbc.gridy = 4;
            boardLabel.add(knightLabel, gbc);
        }

        protected JComponent filler() {
            JLabel filler = new JLabel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(50, 50);
                }
            };
            return filler;
        }

    }

}

With a setup like this, you can simply use GridBagConstraint's gridx and gridy values as direct cell address (no need to calculate the pixel position).

Because of the way the layout manager works, you are required to provide an empty cell component as well

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0
JLabel background1 = new JLabel(new ImageIcon("background.png"));
JLabel knight=new JLabel(new ImageIcon("knight.jpg"));
frame.add(background1);
frame.add(knight);

I'm guessing what you really want is have the knight displayed on top (on the z axis) so you need to do something like:

JLabel background1 = new JLabel(new ImageIcon("background.png"));
background.setLayout( new BorderLayout() );
JLabel knight=new JLabel(new ImageIcon("knight.jpg"));
background1.add(knight);
frame.add(background1);

That is you need to follow a parent/child hierarchy:

  1. add the background to the frame
  2. add the knight to the background
camickr
  • 321,443
  • 19
  • 166
  • 288
  • just to clarify if i wanted multiple images to be on the same level of the hierarchy i would add them to background1.add(additionalIamgae)l – thebigone Feb 29 '16 at 22:06
  • @thebigone, yes, but you would use a different layout manager on your background label (maybe a FlowLayout). This is the basis for all of Swing components. They follow a parent / child relationship. So anytime you want multiple components on one panel you add all the component to the same panel. The layout manager will then arrange the components based on the rules of the layout manager. – camickr Feb 29 '16 at 22:07
  • You can also check out the [Background Panel](https://tips4java.wordpress.com/2008/10/12/background-panel/) which provides more flexibility in how the background is painted. – camickr Feb 29 '16 at 22:13