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 631179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:56:12+00:00 2026-05-13T19:56:12+00:00

I’m working on a GUI for a chess game and I was wondering if

  • 0

I’m working on a GUI for a chess game and I was wondering if there’s any way to check for a series of clicks, for example: user clicks on jPanel THEN user clicks on another jPanel that exists in valid move array. I know I could use a variable to store some kind of state like “isSquareClicked = true” or something but I’d rather not unless that’s the only way…

  • 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-05-13T19:56:12+00:00Added an answer on May 13, 2026 at 7:56 pm

    I don’t see anything wrong with using JPanels. Here’s my implementation:

    First a ChessSquare, this represents one cell on the board:

    public class ChessSquare extends JPanel{
        int x,y;
    
        public ChessSquare(int x, int y){
            super();
            this.setPreferredSize(new Dimension(50,50));
            this.setBorder(BorderFactory.createLineBorder(Color.black));
            this.x = x;
            this.y = y;
        }
    }
    

    Now the main board panel:

    public class ChessPanel extends JPanel{
        JPanel positions[][] = new JPanel[8][8];
        ChessSquare move[] = new ChessSquare[2];
    
        public ChessPanel(){
            initComponents();
        }
    
        private void initComponents(){
            setLayout(new GridLayout(8,8));
    
            for(int i=0;i<positions.length;i++){
                for(int j=0;j<positions[i].length;j++){
                    ChessSquare square = new ChessSquare(i,j);
                    square.addMouseListener(new MouseListener(){
                        public void mouseClicked(MouseEvent me) {
                            ChessSquare cs = (ChessSquare)me.getComponent();
                            if(isValidMove(cs)){
    
                                System.out.println("Valid move!");
                                System.out.println("x1: "+move[0].x+" y1: "+move[0].y);
                                System.out.println("x2: "+move[1].x+" y2: "+move[1].y);
                                System.out.println("");
    
                                resetMove();
                            }
                        }
    
                        //Other mouse events
    
                    });
                    positions[i][j] = square;
                    add(square);
                }
            }
        }
    
        private boolean isValidMove(ChessSquare square){
            //here you would check if the move is valid.
            if(move[0] == null){
                move[0] = square;
                return false; //first click
            }else{
                move[1] = square;
            }
    
            //Other chess rules go here...
            return true;
        }
    
        private void resetMove(){
            move = new ChessSquare[2];
        }
    }
    

    We keep a JPanel matrix to represent the board, and ChessSquare array to represent the current move. In isValidMove() we check to see if the current move is complete (both squares have been clicked, thus the move array already has one element). Once a move is complete, we reset the move and start again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I need to clean up various Word 'smart' characters in user input, including but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.