Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:32:09+00:00 2026-06-11T17:32:09+00:00

I’m having a problem while trying to make my own chess game using JLayeredPane

  • 0

I’m having a problem while trying to make my own chess game using JLayeredPane.

I got this far:

No side panels

(Hopefully you can see block G6 has a green border on the label showing which block was selected)

But when I add my 2 SidePanels to my ChessBoard panel and then have another layer on top with labels that should cover each of the boards blocks exactly but it doesn’t:

With sidepanels

as you can see the green Border surrounding block G2 is off.

I have narrowed it down to the obvious when I add the SidePanels to the ChessBoard and add that to the bottom layer and set its size to say 600×600 then add the top layer which is labels that will fit the blocks of the ChessBoard exactly, and the green border is drawn around the selected JLabel (and also the lower ChessBoard square [black or white]) it is drawn off center due to the SidePanels which have decreased the chessboards actual size of 600×600, now it would be 600-sp1.getWidth()x600-sp2.getHeight(). I tried setting the bounds and preferred size of the top layer to compensate for it but it seems no use. Any help is appreciated thank you:

ChessBoardTest.java:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class ChessBoardTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                Dimension boardSize = new Dimension(600, 600);

                JFrame frame = new JFrame("Chess JLayeredPane Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);

                Container contentPane = frame.getContentPane();

                ChessBoard chessBoard = new ChessBoard();
                SidePanel sp1 = new SidePanel(new String[]{"8", "7", "6", "5", "4", "3", "2", "1"}, SidePanel.VERTICAL);
                SidePanel sp2 = new SidePanel(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"}, SidePanel.HORIZONTAL);

                //adding these 2 side panels messes up the layout
                chessBoard.add(sp1, BorderLayout.WEST);
                chessBoard.add(sp2, BorderLayout.SOUTH);

                chessBoard.setPreferredSize(boardSize);
                chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

                ChessPieceLayer chessPieceLayer = new ChessPieceLayer();

            //chessPieceLayer.setPreferredSize(boardSize);
            //chessPieceLayer.setBounds(0, 0, boardSize.width, boardSize.height);

            //i've tried resizing to make up for the side panels but no result
            chessPieceLayer.setPreferredSize(new Dimension(600-sp1.getWidth(),600-sp2.getHeight()));
            chessPieceLayer.setBounds(0+sp1.getWidth(), 0+sp2.getHeight(), 600-sp1.getWidth(), 600-sp2.getHeight());




                JLayeredPane jLayeredPane = new JLayeredPane();
                jLayeredPane.setPreferredSize(boardSize);

                jLayeredPane.add(chessBoard, JLayeredPane.FRAME_CONTENT_LAYER);
                jLayeredPane.add(chessPieceLayer, JLayeredPane.MODAL_LAYER);

                contentPane.add(jLayeredPane);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

SidePanel.java:

class SidePanel extends JPanel {

    final static String HORIZONTAL = "horizontal";
    final static String VERTICAL = "vertical";

    public SidePanel(String[] strings, String direction) {
        if (direction.equals(VERTICAL)) {
            setLayout(new GridLayout(8, 0));
        } else {
            setLayout(new GridLayout(0, 8));
        }
        setDoubleBuffered(true);
        for (String string : strings) {
            this.add(new JLabel(string, JLabel.CENTER));
        }

    }
}

ChessBoard.java:

class ChessBoard extends JPanel {

    public ChessBoard() {
        super(new BorderLayout(), true);

        this.add(populateBoard(Color.white, Color.black), BorderLayout.CENTER);
    }

    private JPanel populateBoard(Color c1, Color c2) {
        JPanel panel = new JPanel(new GridLayout(8, 8));
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                JPanel square = new JPanel(new BorderLayout());
                square.setBackground((i + j) % 2 == 0 ? c1 : c2);
                panel.add(square);
            }
        }
        return panel;
    }
}

ChessPieceLayer.java:

class ChessPieceLayer extends JComponent {

    private HashMap<PiecePanel, String> panelsMap = new HashMap<>(64);
    final ChessPieceMouseListener listener;

    ChessPieceLayer() {
        super();
        listener = new ChessPieceMouseListener();
        setLayout(new GridLayout(8, 8));
        setDoubleBuffered(true);

        fillPanelsMap();
    }

    private void fillPanelsMap() {
        String[] cols = new String[]{"A", "B", "C", "D", "E", "F", "G", "H"};
        int[] rows = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        String row, col;
        int rowCount = 7, colCount = 0, trigger = 8;

        for (int i = 0; i < 64; i++) {

            if (trigger == 0) {
                colCount = 0;
                trigger = 8;
                rowCount--;
            }
            col = cols[colCount++];
            row = rows[rowCount] + "";
            trigger--;

            String location = col + row;

            PiecePanel square = createAndAddPiecesWithMouseListener(location);

            panelsMap.put(square, location);

        }
    }

    private PiecePanel createAndAddPiecesWithMouseListener(String location) {
        PiecePanel square = new PiecePanel(location, JLabel.CENTER);
        square.addMouseListener(listener);
        square.setText(location);
        this.add(square);
        return square;
    }
}

PiecePanel.java:

class PiecePanel extends JLabel {

    private String location;

    public PiecePanel(String text, int horizontalAlignment) {
        super("", horizontalAlignment);
        this.location = text;
    }

    PiecePanel(String location) {
        this.location = location;
    }

    public String getPieceLocation() {
        return location;
    }

    public void setPieceLocation(String location) {
        this.location = location;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}

ChessPieceMouseListener.java:

class ChessPieceMouseListener implements MouseListener {

    int counter = 0;
    PiecePanel this_pp, prev_pp;

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        PiecePanel pp = (PiecePanel) e.getComponent();
        if (counter == 0) {
            this_pp = pp;
            this_pp.setBorder(new BevelBorder(0, Color.green, Color.green));
            JOptionPane.showMessageDialog(null, "From " + pp.getPieceLocation());
            counter = 1;
        } else {
            prev_pp = this_pp;
            this_pp = pp;
            prev_pp.setBorder(null);
            JOptionPane.showMessageDialog(null, "To " + pp.getPieceLocation());
            counter = 0;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}

at first I used:

chessPieceLayer.setPreferredSize(boardSize);
chessPieceLayer.setBounds(0, 0, boardSize.width, boardSize.height);

Then I tried:     

chessPieceLayer.setPreferredSize(new Dimension(600-sp1.getWidth(),600-sp2.getHeight()));
chessPieceLayer.setBounds(0+sp1.getWidth(), 0+sp2.getHeight(), 600-sp1.getWidth(), 600-sp2.getHeight());

but the result doesnt differ

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:32:11+00:00Added an answer on June 11, 2026 at 5:32 pm

    Ok, I found the problem and there is an easy solution. Next time, try to put your code in a single class so that I don’t have to do so many Copy/Paste (this kind of discourage people to try your code) ;-).

    Anyway, the problem comes from your SidePanel which takes space on your ChessBoardPanel and therefore introduces an offset. The solution is quite easy, all you have to do is:

    1. add sp1 and sp2 to contentPane
    2. on your chessBoard and chessPieceLayer simply set the size to boardSize.

    Explanation

    1. we move the SidePanel to the content pane so that they don’t interfere with your layers. When you use a JLayeredPane, you use what is called absolute-layout or null-layout. This means that you have to take care of the positioning and sizing of your components. This leads me to point 2:
    2. When there is no LayoutManager, setting the preferredSize is useless, since there is no LayoutManager to call that method. Now, by default components are located in (0,0), so all you have to do is call setSize. If you want them to be offset-ed, you need to also call setLocation, or concatenate these 2 calls in a single call to setBounds
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.