Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);
        layeredPane.addMouseListener(this);
        layeredPane.addMouseMotionListener(this);

        //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 );
            else
                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);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/chess1.jpg"));
        panel = (JPanel)chessBoard.getComponent(15);
        panel.add(piece);
        piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/king.jpg"));
        panel = (JPanel)chessBoard.getComponent(16);
        panel.add(piece);
  piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/camel.jpg"));
        panel = (JPanel)chessBoard.getComponent(20);
        panel.add(piece);

    }
 
    public void mousePressed(MouseEvent e){
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
 
        if (instanceof JPanel
  return;
 
        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;
 
        chessPiece.setVisible(false);
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
 
        if (instanceof JLabel){
            Container parent = c.getParent();
            parent.remove(0);
            parent.addchessPiece );
        }
        else {
            Container parent = (Container)c;
            parent.addchessPiece );
        }
 
        chessPiece.setVisible(true);
    }
 
    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.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTonull );
        frame.setVisible(true);
     }
}

 

Output  is the Program

     
Here is the Download

                         

» View all related tutorials
Related Tags: java c ide class graphics constructor object io struct classes size 2d graph const ip vi trac awt int this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.