Home Java Example Java Swing Chess Application In Java Swing

Related Tutorials


Share on Google+Share on Google+

Chess Application In Java Swing

In this section, you will learn how to create chess game in java swing.

Chess Application In Java Swing


In this section, you will learn how to create chess game in java swing.

In the given example, we have defined MouseListener, MouseMotionListener perform two actions. This chess simulator will help you to master your chess playing skills, to train your intellectual capacity and give you lots of entertainment.  Mouse events notify when the user uses the mouse to interact with a component. Mouse events occur when the cursor enters or exits a component's on screen area and when the user presses or releases one of the mouse buttons.

Here is the Code.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessGameDemo extends JFrame implements MouseListener, MouseMotionListener {
  JLayeredPane layeredPane;
  JPanel chessBoard;
  JLabel chessPiece;
  int xAdjustment;
  int yAdjustment;
  public ChessGameDemo(){
  Dimension boardSize = new Dimension(600600);
  //  Use a Layered Pane for this this application
 layeredPane = new JLayeredPane();

  //Add a chess board to the Layered Pane 
  chessBoard = new JPanel();
  layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
  chessBoard.setLayoutnew GridLayout(88) );
  chessBoard.setPreferredSizeboardSize );
  chessBoard.setBounds(00, boardSize.width, boardSize.height);
  for (int i = 0; i < 64; i++) {
  JPanel square = new JPanelnew BorderLayout() );
  chessBoard.addsquare );
  int row = (i / 82;
  if (row == 0)
  square.setBackgroundi % == ? Color.blue : Color.white );
  square.setBackgroundi % == ? Color.white : Color.blue );
  //Add a few pieces to the board
  JLabel piece = new JLabelnew ImageIcon("/home/vinod/amarexamples/chess.jpg") );
  JPanel panel = (JPanel)chessBoard.getComponent(0);
  piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/chess1.jpg"));
  panel = (JPanel)chessBoard.getComponent(15);
  piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/king.jpg"));
  panel = (JPanel)chessBoard.getComponent(16);
  piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/camel.jpg"));
  panel = (JPanel)chessBoard.getComponent(20);

  public void mousePressed(MouseEvent e){
  chessPiece = null;
  Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
  if (instanceof JPanel
  Point parentLocation = c.getParent().getLocation();
  xAdjustment = parentLocation.x - e.getX();
  yAdjustment = parentLocation.y - e.getY();
  chessPiece = (JLabel)c;
  chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
  chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
  layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
  //Move the chess piece around
  public void mouseDragged(MouseEvent me) {
  if (chessPiece == nullreturn;
 chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
  //Drop the chess piece back onto the chess board
  public void mouseReleased(MouseEvent e) {
  if(chessPiece == nullreturn;
  Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
  if (instanceof JLabel){
  Container parent = c.getParent();
  parent.addchessPiece );
  else {
  Container parent = (Container)c;
  parent.addchessPiece );
  public void mouseClicked(MouseEvent e) {
  public void mouseMoved(MouseEvent e) {
  public void mouseEntered(MouseEvent e){
  public void mouseExited(MouseEvent e) {
  public static void main(String[] args) {
  JFrame frame = new ChessGameDemo();
  frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
  frame.setLocationRelativeTonull );


Output  is the Program

Here is the Download


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2008

Related Tutorials

Discuss: Chess Application In Java Swing   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 1, 2011

thank you it's very simple and easy
Marcelo Garca
November 24, 2011
Thanks a Lot

Your code help me a lot! I made a chess - text mode, and now i need to convert for swing. I will use your board at swing. Tks a lot!
March 1, 2013

aweosme code! thanks a ton!! :*
July 16, 2012

I receive the following error when attempting to compile this code: ChessGameDemo.java:120: cannot resolve symbol symbol : method setLocationRelativeTo (<null>) location: class javax.swing.JFrame frame.setLocationRelativeTo( null ); ^ 1 error
September 1, 2012
Very Use Full to fressers

Thanks to every one to giving such information.
November 17, 2012
Thanks and correction

Thank you very much for this useful code. The correction is that you should change this code: ///////////////////// if (row == 0) square.setBackground( i % 2 == 0 ? Color.blue : Color.white ); else square.setBackground( i % 2 == 0 ? Color.white : Color.blue ); } //////////////////// To be: /////////////////// if (row == 0) // For odd square square.setBackground( i % 2 == 0 ? Color.white : Color.gray ); else square.setBackground( i % 2 == 0 ? Color.gray : Color.white ); } ///////////////// Because the chess board must start with white upper left corner and end with white lower right corner.