console to GUI

I'm attempting to move my console-based chess game to a GUI. I'm currently referencing the this chess tutorial to help get me started. I'm currently setting only one white rook on the board for testing purposes. In my console chess program, I have a Rook Class that determines how the Rook can move, what color it is, and so on.

In the tutorial code, they add a piece to the board like so:

    // Add a few pieces to the board    
    JLabel piece = new JLabel("rl.png");
    JPanel panel = (JPanel) chessBoard.getComponent(0);
    panel.add(piece);

//  my Attempt ---------------------------------------------------//

//      String rook = "Rook";
//      ImageIcon image4 = new ImageIcon("rl.png");
//      Rook firstRook = new Rook(rook, image4);
//      
//      piece = new JLabelfirstRook);
//      panel = (JPanel) chessBoard.getComponent(0);
//      panel.add(piece);

The rook can then be moved all over the board as it does not have any move rules applying to it. I want to hook up my rook class to the JLabel piece, so that the rook is bound by the move rules. Thus, my question:

How can I hook up my console Rook Class to the Rook on the board? And is there is a better way of doing this? Any help is appreciated, I've already scoured the internet for help on this and haven't been successful. Thanks!

My Rook Class:

public class RookTest extends ChessPiece
{ 

String name;
ImageIcon visualRep; //= new ImageIcon("rl.png");

public RookTest(String name, ImageIcon visualRep)
{
    this.name = name;
    this.visualRep = visualRep; 
}   

public String setInBoard() {
    return "| " + name + " |";
}


public String getName()
{
    return name;
}

public String getColor()
{
    String pieceColor = null;
    if (name.equals("r"))
    {
        pieceColor = "d";
    }
    else if (name.equals("R"))
    {
        pieceColor = "l";
    }
    return pieceColor;  
}

public String toString() {
    //something to find the color in here
    return "Rook";
}

//legal moves
//-------------------------------------------------------------------------------------------------------//

public boolean legalMove(ChessPiece[][] chess, int startY, int startX, int endY, int endX) 
{
    boolean thisIsAValidMove = false;

    //MOVING NORTH-SOUTH //if its in the same column and not in the same row//
    if (startY == endY && startX != endX ) 
        thisIsAValidMove = true;

    //MOVING EAST-WEST // if its in the same row but not the same column//
    else if(startX == endX && startY != endY)
        thisIsAValidMove = true;
    else
        thisIsAValidMove = false;
    return thisIsAValidMove;
}
//-------------------------------------------------------------------------------------------------------//
链接地址: http://www.djcxy.com/p/84650.html

上一篇: 将国际象棋引擎与C ++ GUI程序连接起来

下一篇: 控制台到GUI