Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将MouseEvents从一个Java文件链接到另一个Java文件_Java_Swing_Mouseevent_Chess - Fatal编程技术网

将MouseEvents从一个Java文件链接到另一个Java文件

将MouseEvents从一个Java文件链接到另一个Java文件,java,swing,mouseevent,chess,Java,Swing,Mouseevent,Chess,一些背景我正在用java开发一个游戏,我正在使用Netbeans来构建它。我目前有3个java文件 App.java Board.java Piece.java 目前,当它运行时,它向用户显示一个简单的棋盘,所有棋子都在正确的位置等 这些都是在Board.java中完成的 我的问题是棋子的移动是在棋子中完成的。java目前我有白色棋子的编码和控制它的鼠标事件,但当我运行程序时,棋盘会与棋子一起出现,但没有一个棋子移动,甚至白色棋子也没有移动 我很难理解为什么,我想也许我没有把它们和黑板或者其

一些背景我正在用java开发一个游戏,我正在使用Netbeans来构建它。我目前有3个java文件

  • App.java
  • Board.java
  • Piece.java
目前,当它运行时,它向用户显示一个简单的棋盘,所有棋子都在正确的位置等

这些都是在Board.java中完成的

我的问题是棋子的移动是在棋子中完成的。java目前我有白色棋子的编码和控制它的鼠标事件,但当我运行程序时,棋盘会与棋子一起出现,但没有一个棋子移动,甚至白色棋子也没有移动

我很难理解为什么,我想也许我没有把它们和黑板或者其他类似的东西联系起来,或者也许我的鼠标事件中有u

此外,我的代码在运行时没有出现任何错误

下面是java文件

App.java

package chessgame;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class App {    
    public static void main(String[] args) {
        JFrame frame = new Board();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
 }
Board.java

package chessgame;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Board extends JFrame {

    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    int startX;
    int startY;
    int initialX;
    int initialY;
    JPanel panels;
    JLabel pieces;

    public Board() {
        Dimension boardSize = new Dimension(600, 600);

        //  This is a Layered Pane for this application
        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);

        //Add a chess board to the Layered Pane
        chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout(new GridLayout(8, 8));
        chessBoard.setPreferredSize(boardSize);
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel(new BorderLayout());
            chessBoard.add(square);

            int row = (i / 8) % 2;
            if (row == 0) {
                square.setBackground(i % 2 == 0 ? Color.white : Color.gray);
            } else {
                square.setBackground(i % 2 == 0 ? Color.gray : Color.white);
            }
        }
        // Setting up the Initial Chess board.
        //White Side
        for (int i = 8; i < 16; i++) {
            pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhitePawn.png")));
            panels = (JPanel) chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png")));
        panels = (JPanel) chessBoard.getComponent(0);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteKnight.png")));
        panels = (JPanel) chessBoard.getComponent(1);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteKnight.png")));
        panels = (JPanel) chessBoard.getComponent(6);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteBishup.png")));
        panels = (JPanel) chessBoard.getComponent(2);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteBishup.png")));
        panels = (JPanel) chessBoard.getComponent(5);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteKing.png")));
        panels = (JPanel) chessBoard.getComponent(3);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteQueen.png")));
        panels = (JPanel) chessBoard.getComponent(4);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png")));
        panels = (JPanel) chessBoard.getComponent(7);
        panels.add(pieces);

        //Black Side
        for (int i = 48; i < 56; i++) {
            pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackPawn.png")));
            panels = (JPanel) chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackRook.png")));
        panels = (JPanel) chessBoard.getComponent(56);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackKnight.png")));
        panels = (JPanel) chessBoard.getComponent(57);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackKnight.png")));
        panels = (JPanel) chessBoard.getComponent(62);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackBishup.png")));
        panels = (JPanel) chessBoard.getComponent(58);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackBishup.png")));
        panels = (JPanel) chessBoard.getComponent(61);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackKing.png")));
        panels = (JPanel) chessBoard.getComponent(59);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackQueen.png")));
        panels = (JPanel) chessBoard.getComponent(60);
        panels.add(pieces);
        pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/BlackRook.png")));
        panels = (JPanel) chessBoard.getComponent(63);
        panels.add(pieces);
    }
}
包装棋类游戏;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
在关闭时导入静态javax.swing.WindowConstants.DISPOSE\u;
公共类板扩展JFrame{
JLayeredPane layeredPane;
JPanel棋盘;
JLabel棋子;
内部调整;
内部调整;
int startX;
int startY;
int initialX;
int首字母;
JPanel小组;
JLabel片段;
公共委员会(){
尺寸板尺寸=新尺寸(600600);
//这是此应用程序的分层窗格
layeredPane=新的JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
//将棋盘添加到分层窗格
棋盘=新JPanel();
layeredPane.add(棋盘,JLayeredPane.DEFAULT_层);
棋盘布局(新网格布局(8,8));
棋盘。设置首选大小(棋盘大小);
棋盘.立根(0,0,boardSize.width,boardSize.height);
对于(int i=0;i<64;i++){
JPanel square=newjpanel(newborderlayout());
棋盘。加(正方形);
int行=(i/8)%2;
如果(行==0){
正方形。背景(i%2==0?颜色。白色:颜色。灰色);
}否则{
正方形。背景(i%2==0?颜色。灰色:颜色。白色);
}
}
//设置初始棋盘。
//白面
对于(int i=8;i<16;i++){
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhitePawn.png”));
panels=(JPanel)chessBoard.getComponent(i);
面板。添加(个);
}
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteRook.png”));
panels=(JPanel)chessBoard.getComponent(0);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteKnight.png”));
panels=(JPanel)chessBoard.getComponent(1);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteKnight.png”));
panels=(JPanel)chessBoard.getComponent(6);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteBishup.png”));
panels=(JPanel)chessBoard.getComponent(2);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteBishup.png”));
panels=(JPanel)chessBoard.getComponent(5);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteKing.png”));
panels=(JPanel)chessBoard.getComponent(3);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteQueen.png”));
panels=(JPanel)chessBoard.getComponent(4);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/WhiteRook.png”));
panels=(JPanel)chessBoard.getComponent(7);
面板。添加(个);
//黑面
对于(int i=48;i<56;i++){
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackPawn.png”));
panels=(JPanel)chessBoard.getComponent(i);
面板。添加(个);
}
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackRook.png”));
panels=(JPanel)chessBoard.getComponent(56);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackKnight.png”));
panels=(JPanel)chessBoard.getComponent(57);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackKnight.png”));
panels=(JPanel)chessBoard.getComponent(62);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackBishup.png”));
panels=(JPanel)chessBoard.getComponent(58);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackBishup.png”));
panels=(JPanel)chessBoard.getComponent(61);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackKing.png”));
panels=(JPanel)chessBoard.getComponent(59);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackQueen.png”));
panels=(JPanel)chessBoard.getComponent(60);
面板。添加(个);
片段=新片段(新图像图标(getClass().getResource(“/chessgame/PieceImages/BlackRook.png”));
panels=(JPanel)chessBoard.getComponent(63)
    package chessgame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Piece extends JLabel implements MouseListener, MouseMotionListener {

    public Piece(ImageIcon icon) { super(icon); }

    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    int startX;
    int startY;
    int initialX;
    int initialY;
    JPanel panels;
    JLabel pieces;

    /*
     This method checks if there is a piece present on a particular square.
     */
    private Boolean piecePresent(int x, int y) {
        Component c = chessBoard.findComponentAt(x, y);
        if (c instanceof JPanel) {
            return false;
        } else {
            return true;
        }
    }

    /*
     This is a method to check if a piece is a Black piece.
     */
    private Boolean checkWhiteOponent(int newX, int newY) {
        Boolean oponent;
        Component c1 = chessBoard.findComponentAt(newX, newY);
        JLabel awaitingPiece = (JLabel) c1;
        String tmp1 = awaitingPiece.getIcon().toString();
        if (((tmp1.contains("Black")))) {
            oponent = true;
        } else {
            oponent = false;
        }
        return oponent;
    }

    /*
     This method is called when we press the Mouse. So we need to find out what piece we have 
     selected. We may also not have selected a piece!
     */
    public void mousePressed(MouseEvent e) {
        chessPiece = null;
        Component c = chessBoard.findComponentAt(e.getX(), e.getY());
        if (c instanceof JPanel) {
            return;
        }

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel) c;
        initialX = e.getX();
        initialY = e.getY();
        startX = (e.getX() / 75);
        startY = (e.getY() / 75);
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    }

    public void mouseDragged(MouseEvent me) {
        if (chessPiece == null) {
            return;
        }
        chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
    }

    /*
     This method is used when the Mouse is released...we need to make sure the move was valid before 
     putting the piece back on the board.
     */
    public void mouseReleased(MouseEvent e) {
        if (chessPiece == null) {
            return;
        }

        chessPiece.setVisible(false);
        Boolean success = false;
        Component c = chessBoard.findComponentAt(e.getX(), e.getY());
        String tmp = chessPiece.getIcon().toString();
        String pieceName = tmp.substring(0, (tmp.length() - 4));
        Boolean validMove = false;

        if (pieceName.equals("WhitePawn")) {
            if (startY == 1) {
                if ((startX == (e.getX() / 75)) && ((((e.getY() / 75) - startY) == 1) || ((e.getY() / 75) - startY) == 2)) {
                    if ((((e.getY() / 75) - startY) == 2)) {
                        if ((!piecePresent(e.getX(), (e.getY()))) && (!piecePresent(e.getX(), (e.getY() + 75)))) {
                            validMove = true;
                        } else {
                            validMove = false;
                        }
                    } else {
                        if ((!piecePresent(e.getX(), (e.getY())))) {
                            validMove = true;
                        } else {
                            validMove = false;
                        }
                    }
                } else {
                    validMove = false;
                }
            } else {
                int newY = e.getY() / 75;
                int newX = e.getX() / 75;
                if ((startX - 1 >= 0) || (startX + 1 <= 7)) {
                    if ((piecePresent(e.getX(), (e.getY()))) && ((((newX == (startX + 1) && (startX + 1 <= 7))) || ((newX == (startX - 1)) && (startX - 1 >= 0))))) {
                        if (checkWhiteOponent(e.getX(), e.getY())) {
                            validMove = true;
                            if (startY == 6) {
                                success = true;
                            }
                        } else {
                            validMove = false;
                        }
                    } else {
                        if (!piecePresent(e.getX(), (e.getY()))) {
                            if ((startX == (e.getX() / 75)) && ((e.getY() / 75) - startY) == 1) {
                                if (startY == 6) {
                                    success = true;
                                }
                                validMove = true;
                            } else {
                                validMove = false;
                            }
                        } else {
                            validMove = false;
                        }
                    }
                } else {
                    validMove = false;
                }
            }
        }
        if (!validMove) {
            int location = 0;
            if (startY == 0) {
                location = startX;
            } else {
                location = (startY * 8) + startX;
            }
            String pieceLocation = pieceName + ".png";
            pieces = new JLabel(new ImageIcon(pieceLocation));
            panels = (JPanel) chessBoard.getComponent(location);
            panels.add(pieces);
        } else {
            if (success) {
                int location = 56 + (e.getX() / 75);
                if (c instanceof JLabel) {
                    Container parent = c.getParent();
                    parent.remove(0);
                    pieces = new JLabel(new ImageIcon("WhiteQueen.png"));
                    parent = (JPanel) chessBoard.getComponent(location);
                    parent.add(pieces);
                } else {
                    Container parent = (Container) c;
                    pieces = new JLabel(new ImageIcon("WhiteQueen.png"));
                    parent = (JPanel) chessBoard.getComponent(location);
                    parent.add(pieces);
                }
            } else {
                if (c instanceof JLabel) {
                    Container parent = c.getParent();
                    parent.remove(0);
                    parent.add(chessPiece);
                } else {
                    Container parent = (Container) c;
                    parent.add(chessPiece);
                }
                chessPiece.setVisible(true);
            }
        }
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/*
    This class can be used as a starting point for creating your Chess game project. The only piece that 
    has been coded is a white pawn...a lot done, more to do!
*/

public class ChessProject extends JFrame implements MouseListener, MouseMotionListener {
    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    int startX;
    int startY;
    int initialX;
    int initialY;
    JPanel panels;
    JLabel pieces;


    public ChessProject(){
        Dimension boardSize = new Dimension(600, 600);

        //  Use a Layered Pane for 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.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel( new BorderLayout() );
            chessBoard.add( square );

            int row = (i / 8) % 2;
            if (row == 0)
                square.setBackground( i % 2 == 0 ? Color.white : Color.gray );
            else
                square.setBackground( i % 2 == 0 ? Color.gray : Color.white );
        }

        // Setting up the Initial Chess board.
        for(int i=8;i < 16; i++){           
            pieces = new JLabel( new ImageIcon("WhitePawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);         
        }
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(0);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(1);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(6);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(2);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(5);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKing.png") );
        panels = (JPanel)chessBoard.getComponent(3);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
        panels = (JPanel)chessBoard.getComponent(4);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(7);
        panels.add(pieces);
        for(int i=48;i < 56; i++){          
            pieces = new JLabel( new ImageIcon("BlackPawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);         
        }
        pieces = new JLabel( new ImageIcon("BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(56);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(57);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(62);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(58);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(61);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKing.png") );
        panels = (JPanel)chessBoard.getComponent(59);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackQueen.png") );
        panels = (JPanel)chessBoard.getComponent(60);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(63);
        panels.add(pieces);     
    }

    /*
        This method checks if there is a piece present on a particular square.
    */
    private Boolean piecePresent(int x, int y){
        Component c = chessBoard.findComponentAt(x, y);
        if(c instanceof JPanel){
            return false;
        }
        else{
            return true;
        }
    }

    /*
        This is a method to check if a piece is a Black piece.
    */
    private Boolean checkWhiteOponent(int newX, int newY){
        Boolean oponent;
        Component c1 = chessBoard.findComponentAt(newX, newY);
        JLabel awaitingPiece = (JLabel)c1;
        String tmp1 = awaitingPiece.getIcon().toString();           
        if(((tmp1.contains("Black")))){
            oponent = true;
        }
        else{
            oponent = false; 
        }       
        return oponent;
    }   

    /*
        This method is called when we press the Mouse. So we need to find out what piece we have 
        selected. We may also not have selected a piece!
    */
    public void mousePressed(MouseEvent e){
        chessPiece = null;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
        if (c instanceof JPanel) 
            return;

        Point parentLocation = c.getParent().getLocation();
        xAdjustment = parentLocation.x - e.getX();
        yAdjustment = parentLocation.y - e.getY();
        chessPiece = (JLabel)c;
        initialX = e.getX();
        initialY = e.getY();
        startX = (e.getX()/75);
        startY = (e.getY()/75);
        chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
        chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
        layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    }

    public void mouseDragged(MouseEvent me) {
        if (chessPiece == null) return;
         chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
     }

    /*
        This method is used when the Mouse is released...we need to make sure the move was valid before 
        putting the piece back on the board.
    */
    public void mouseReleased(MouseEvent e) {
        if(chessPiece == null) return;

        chessPiece.setVisible(false);
        Boolean success =false;
        Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
        String tmp = chessPiece.getIcon().toString();
        String pieceName = tmp.substring(0, (tmp.length()-4));
        Boolean validMove = false;

        /*
            The only piece that has been enabled to move is a White Pawn...but we should really have this is a separate
            method somewhere...how would this work.

            So a Pawn is able to move two squares forward one its first go but only one square after that. 
            The Pawn is the only piece that cannot move backwards in chess...so be careful when committing 
            a pawn forward. A Pawn is able to take any of the opponent’s pieces but they have to be one 
            square forward and one square over, i.e. in a diagonal direction from the Pawns original position. 
            If a Pawn makes it to the top of the other side, the Pawn can turn into any other piece, for 
            demonstration purposes the Pawn here turns into a Queen.
        */
        if(pieceName.equals("WhitePawn")){
            if(startY == 1)
            {
                if((startX == (e.getX()/75))&&((((e.getY()/75)-startY)==1)||((e.getY()/75)-startY)==2))
                {
                    if((((e.getY()/75)-startY)==2)){
                        if((!piecePresent(e.getX(), (e.getY())))&&(!piecePresent(e.getX(), (e.getY()+75)))){
                            validMove = true;                   
                        }
                        else{
                            validMove = false;
                        }                           
                    }
                    else{
                        if((!piecePresent(e.getX(), (e.getY()))))
                        {
                            validMove = true;                   
                        }   
                        else{
                            validMove = false;
                        }                                                   
                    }
                }
                else{
                    validMove = false;                  
                }
            }
            else{
                int newY = e.getY()/75;
                int newX = e.getX()/75;             
                if((startX-1 >=0)||(startX +1 <=7))
                {
                    if((piecePresent(e.getX(), (e.getY())))&&((((newX == (startX+1)&&(startX+1<=7)))||((newX == (startX-1))&&(startX-1 >=0)))))
                    {
                        if(checkWhiteOponent(e.getX(), e.getY())){
                            validMove = true;
                            if(startY == 6){
                                success = true;
                            }                       
                        }
                        else{
                            validMove = false;
                        }
                    }
                    else{
                        if(!piecePresent(e.getX(), (e.getY()))){
                            if((startX == (e.getX()/75))&&((e.getY()/75)-startY)==1){
                                if(startY == 6){
                                    success = true;
                                }
                                validMove = true;
                            }
                            else{
                                validMove = false;
                            }               
                        }
                        else{
                            validMove = false;  
                        }
                    }
                }
                else{
                    validMove = false;
                }               
            }           
        }
        if(!validMove){     
            int location=0;
            if(startY ==0){
                location = startX;
            }
            else{
                location  = (startY*8)+startX;
            }
            String pieceLocation = pieceName+".png"; 
            pieces = new JLabel( new ImageIcon(pieceLocation) );
            panels = (JPanel)chessBoard.getComponent(location);
            panels.add(pieces);         
        }
        else{
            if(success){
                int location = 56 + (e.getX()/75);
                if (c instanceof JLabel){
                    Container parent = c.getParent();
                    parent.remove(0);
                    pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
                    parent = (JPanel)chessBoard.getComponent(location);
                    parent.add(pieces);         
                }
                else{
                    Container parent = (Container)c;
                    pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
                    parent = (JPanel)chessBoard.getComponent(location);
                    parent.add(pieces);                 
                }
            }
            else{
                if (c instanceof JLabel){
                    Container parent = c.getParent();
                    parent.remove(0);
                    parent.add( chessPiece );
                }
                else {
                    Container parent = (Container)c;
                    parent.add( chessPiece );
                }
                chessPiece.setVisible(true);                                    
            }
        }
    }

    public void mouseClicked(MouseEvent e) {

    }
    public void mouseMoved(MouseEvent e) {
   }
    public void mouseEntered(MouseEvent e){

    }
    public void mouseExited(MouseEvent e) {

    }

    /*
        Main method that gets the ball moving.
    */
    public static void main(String[] args) {
        JFrame frame = new ChessProject();
        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
        frame.pack();
        frame.setResizable(true);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
     }
}
pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png")));
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
//...
JPanel panels;
JLabel pieces;
public Piece(Icon icon) { 
    super(icon); 
}
pieces = new Piece(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png")));
pieces.addMouseListener(this);