Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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
Java Chess接口与pieceimg实现_Java_Swing - Fatal编程技术网

Java Chess接口与pieceimg实现

Java Chess接口与pieceimg实现,java,swing,Java,Swing,我目前正在为我的数据结构类制作一个国际象棋程序。我们基本上已经完成了大部分代码,但由于某些原因,我不知道如何将这些片段放到任何帮助上。 我们正在使用JAVA和Swing 主要目标: 让工件IMG进入电路板: public abstract class UserInterface { //FIELDS protected Board board; //The board that the UI will be used to interact with //CONSTRUCTORS pub

我目前正在为我的数据结构类制作一个国际象棋程序。我们基本上已经完成了大部分代码,但由于某些原因,我不知道如何将这些片段放到任何帮助上。 我们正在使用JAVA和Swing

主要目标: 让工件IMG进入电路板:

public abstract class UserInterface {

//FIELDS

protected Board board;
//The board that the UI will be used to interact with

//CONSTRUCTORS

public UserInterface(Board board) {
    //Constructor for UserInterface implementers

    this.board = board;

}

public UserInterface() {

}

//OTHER

public abstract Move promptMove();

public abstract PieceType promptPromotion();

public abstract void updateBoard();

public abstract void playGame(Interactable whiteUser, Interactable blackUser);

}

class CommandInterface extends UserInterface {

private GraphicInterface gui = null;

//CONSTRUCTORS

public CommandInterface(Board board) {
    //Main constructor for the CommandInterface

    super(board);
    gui = new GraphicInterface(board, null);
    gui.initializeGui();


}

//OTHER

public void playGame(Interactable whiteUser, Interactable blackUser) {
    //Starts a game using the UI and the board.
    //[TEST CODE] Probably will clean this up later

    board.setUsers(whiteUser, blackUser);
    updateBoard();
    Scanner input = new Scanner(System.in);

    boolean gameOver = false;

    while(!gameOver) {

        boolean illegalMove;

        do {

            try {

//                    System.out.println("Press enter to move");
//                    String s = input.nextLine();

                board.doMove(board.getCurrentUser().getMove());
                illegalMove = false;

            } catch(IllegalArgumentException e) {

                System.out.println(e.getMessage());
                illegalMove = true;

            }

            if(board.getState() == BoardState.CHECKMATE) {

                System.out.println("\n" + board.getTurn().toString() + " is now checkmated. Game over!");

                gameOver = true;
                illegalMove = false;

            } else if(board.getState() == BoardState.CHECK) {

                System.out.println("\n" + board.getTurn().toString() + " has been put in check!");
                illegalMove = false;

            } else if(board.getState() == BoardState.STALEMATE) {

                System.out.println("\n" + board.getTurn().toString() + " is now stalemated. Game over!");
                gameOver = true;
                illegalMove = false;

            }

        } while(illegalMove);

        updateBoard();

    }

}

public Move promptMove() {
    //Takes user input for a move and returns the Move object

    Scanner input = new Scanner(System.in);

    String move;
    boolean again;

    do {

        System.out.print("Enter your move (? for help): ");
        move = input.nextLine();

        if(move.equals("?")) {
            //If the user asks for help

            System.out.printf("%nHELP: To move, you must type a move in the following format:"
                    + "%n<STARTING TILE> <ENDING TILE>"
                    + "%nThe starting tile is the tile of the piece you wish to move."
                    + "%nThe ending tile is the tile you wish to move your piece to."
                    + "%nEach tile is notated with \"<COLUMN><RANK>\", example: \"e5\""
                    + "%n%nFull example move: \"a5 g5\"%n");

            again = true;

        } else if(move.equalsIgnoreCase("CASTLE")) {

            System.out.print("Enter the movement of the King: ");

            move = input.nextLine();

            return new Move(board, move, true);

        } else again = false;

    } while(again);
    //Reprompt if the user asks for help

    return new Move(board, move);
    //Returns a Move object made from the SMN string

}

public PieceType promptPromotion() {

    Scanner input = new Scanner(System.in);
    System.out.print("Pawn has promotion available.\nWhich piece to promote to? ");

    while(true) {

        String type = input.nextLine();

        PieceType promotion = PieceType.NONE;

        if (type.equalsIgnoreCase("PAWN")) promotion = PieceType.PAWN;
        else if (type.equalsIgnoreCase("ROOK")) promotion = PieceType.ROOK;
        else if (type.equalsIgnoreCase("KNIGHT")) promotion = PieceType.KNIGHT;
        else if (type.equalsIgnoreCase("BISHOP")) promotion = PieceType.BISHOP;
        else if (type.equalsIgnoreCase("QUEEN")) promotion = PieceType.QUEEN;
        else if (type.equalsIgnoreCase("KING")) promotion = PieceType.KING;
        else if (type.equalsIgnoreCase("EARL")) promotion = PieceType.EARL;
        else if (type.equalsIgnoreCase("MONK")) promotion = PieceType.MONK;

        if(promotion == PieceType.NONE) {

            System.out.print("\nInvalid type, please try again: ");
            continue;

        }

        return promotion;

    }

}

public void updateBoard() {
    //Prints the board in basic ASCII format.

    gui.updateBoard();

    drawCaptured(Color.WHITE);

    System.out.println();
    //Adds a line before the board starts printing

    int squaresPrinted = 0;
    //Keeps track of the total number of squares that has been printed

    for(int y = board.getZeroSize(); y >= 0; y--) {
        //Loop through y-values from top to bottom

        if(y != board.getZeroSize()) System.out.println();
        //If not on the first iteration of y-loop, go down a line (avoids leading line break)
        System.out.print((y + 1) + " ");
        //Print the rank number

        for(int x = 0; x <= board.getZeroSize(); x++) {
            //Loop through x-values from left to right

            if(board.pieceAt(x, y) != null) {
                //If there is a piece on the tile

                if(squaresPrinted % 2 == 0) System.out.print("[" + board.pieceAt(x, y).getString() + "]");
                    //If squaresPrinted is even, print "[<pString>]"
                else System.out.print("(" + board.pieceAt(x, y).getString() + ")");
                //If squaresPrinted is odd, print "(<pString>)"

            } else {
                //If there is no piece on the tile

                if(squaresPrinted % 2 == 0) System.out.print("[ ]");
                    //If squaresPrinted is even, print "[ ]"
                else System.out.print("( )");
                //If squaresPrinted is odd, print "( )"

            }

            squaresPrinted++;
            //Increment squaresPrinted for each iteration of the x-loop

        }

        squaresPrinted++;
        //Increment squaresPrinted for each iteration of the y-loop

    }

    System.out.println();
    System.out.print(" ");
    //Print an extra line and the leading whitespace for the column identifiers

    for(int i = 0; i <= board.getZeroSize(); i++) {
        //Repeat <size> times

        System.out.print("  " + (char) (i + 97));
        //Print the column identifier chars by casting from int

    }

    drawCaptured(Color.BLACK);
    //Prints all black pieces that have been captured
    System.out.println();

}

private void drawCaptured(Color color) {
    //Prints captured pieces of either color.

    System.out.println();
    //Prints a blank line

    ArrayList<String> capturedPieces = board.getCaptured(color);

    if(capturedPieces == null) return;

    for(String p : capturedPieces) {

        System.out.print(p + " ");
        //TODO: Remove trailing whitespace

    }

}

}

@SuppressWarnings("ALL")
class GraphicInterface extends UserInterface implements MouseListener {

private JButton[][] chessBoardSquares = null;
private JButton buttonsAndLabels = new JButton();
private JLabel message = null;
private String columnNames = null;
private Board board = null;
private JFrame frame;
public ImageIcon BK;
public ImageIcon WP;
public ImageIcon WR;
public ImageIcon WN;
public ImageIcon WB;
public ImageIcon WQ;
public ImageIcon WK;
public ImageIcon BP;
public ImageIcon BR;
public ImageIcon BN;
public ImageIcon BB;
public ImageIcon BQ;
public ImageIcon blank;
private Object ImageIcon;


public GraphicInterface(Board board, GraphicInterface frame) {
    super(board);

//        super(board);

    this.board = board;


    try {
        //remove later
        String path = "src/chuss/icons/";

        new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BK.gif")));
       new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WP.gif")));

        new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WR.gif")));

        WN = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WN.gif")));
        WB = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WB.gif")));
        WQ = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WQ.gif")));
        WK = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "WK.gif")));
        BP = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BP.gif")));
        BR = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BR.gif")));
        BN = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BN.gif")));
        BB = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BB.gif")));
        BQ = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "BQ.gif")));
        blank = new ImageIcon(javax.imageio.ImageIO.read(new File(path + "blank.png")));

        buttonsAndLabels.addMouseListener((MouseListener) this);




        buttonsAndLabels.add(buttonsAndLabels);
        buttonsAndLabels.setSize(600, 600);
        //buttonsAndLabels.getContentPane().setPreferredSize(new Dimension(800, 700));






    } catch(IOException e) {
        e.getCause();
        e.printStackTrace();

    }

    buttonsAndLabels = new JButton((Action) new BorderLayout(3, 3));
    chessBoardSquares = new JButton[8][8];
    message = new JLabel("Chuss Champ is ready to play!");
    columnNames = "ABCDEFGH";

    initializeGui();

}

public GraphicInterface(JButton buttonsAndLabels) {
    super();

}

public GraphicInterface(Board board) {
}

@Override
public Move promptMove() {
    return null;
}

@Override
public PieceType promptPromotion() {
    return null;
}

public final JButton initializeGui() {
    // set up the main GUI
    assert false;
    buttonsAndLabels.setBorder(new EmptyBorder(5, 5, 5, 5));
    JToolBar tools = new JToolBar();
    tools.setFloatable(false);
    buttonsAndLabels.add(tools, BorderLayout.PAGE_START);
    tools.add(new JButton("New")); // TODO - add functionality!
    tools.add(new JButton("Save")); // TODO - add functionality!
    tools.add(new JButton("Restore")); // TODO - add functionality!
    tools.addSeparator();
    tools.add(new JButton("Resign")); // TODO - add functionality!
    tools.addSeparator();
    tools.add(message);

    buttonsAndLabels.add(new JLabel("?"), BorderLayout.LINE_START);
    // create the chess board squares

    buttonsAndLabels = new JButton((Action) new GridLayout(0, 9));
    buttonsAndLabels.setBorder(new LineBorder(java.awt.Color.BLACK));
    buttonsAndLabels.add((Component) ImageIcon);

    updateBoard();

    Runnable r = () -> {

        JFrame f = new JFrame("CHUSS");
        f.add(getGui());
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLocationByPlatform(true);

        // ensures the frame is the minimum size it needs to be
        // in order display the components within it
        f.pack();
        // ensures the minimum size is enforced.
        f.setMinimumSize(f.getSize());
        f.setVisible(true);
    };
    SwingUtilities.invokeLater(r);
    assert false;
    return null;
}

public void updateBoard() {
    //Prints the board in graphical format.

    buttonsAndLabels.removeAll();

    Insets buttonMargin = new Insets(0,0,0,0);

    for (int ii = 7; ii >= 0; ii--) {

        for (int jj = 0; jj < chessBoardSquares.length; jj++) {

            JButton b = new JButton();
            b.setMargin(buttonMargin);
            // our chess pieces are 64x64 px in size, so we'll
            // 'fill this in' using a transparent icon..

            Piece p = board.pieceAt(jj, ii);

            ImageIcon icon = new ImageIcon();

            if (p instanceof Pawn && p.getColor() == Color.WHITE) icon = WP;
            else if (p instanceof Rook && p.getColor() == Color.WHITE) icon = WR;
            else if (p instanceof Knight && p.getColor() == Color.WHITE) icon = WN;
            else if (p instanceof Bishop && p.getColor() == Color.WHITE) icon = WB;
            else if (p instanceof Queen && p.getColor() == Color.WHITE) icon = WQ;
            else if (p instanceof King && p.getColor() == Color.WHITE) icon = WK;
            else if (p instanceof Pawn && p.getColor() == Color.BLACK) icon = BP;
            else if (p instanceof Rook && p.getColor() == Color.BLACK) icon = BR;
            else if (p instanceof Knight && p.getColor() == Color.BLACK) icon = BN;
            else if (p instanceof Bishop && p.getColor() == Color.BLACK) icon = BB;
            else if (p instanceof Queen && p.getColor() == Color.BLACK) icon = BQ;
            else if (p instanceof King && p.getColor() == Color.BLACK) icon = BK;
            else if (p == null) icon = blank;

            b.setIcon(icon);
            if ((jj % 2 == 1 && ii % 2 == 1)
                    //) {
                    || (jj % 2 == 0 && ii % 2 == 0)) {
                b.setBackground(java.awt.Color.WHITE);
            } else {
                b.setBackground(java.awt.Color.GRAY);
            }
            chessBoardSquares[jj][ii] = b;
            buttonsAndLabels.add(chessBoardSquares[jj][ii]);

        }
    }

    initializeGui();

    // fill the top row
    for (int ii = 0; ii <= 7; ii++) {
        buttonsAndLabels.add(
                new JLabel(columnNames.substring(ii, ii + 1),
                        SwingConstants.CENTER));
    }
    // fill the black non-pawn piece row
    for (int ii = 7; ii >= 0; ii--) {
        for (int jj = 0; jj < 8; jj++) {
            if (jj == 0) {
                buttonsAndLabels.add(new JLabel("" + (ii + 1),
                        SwingConstants.CENTER));
            }
            buttonsAndLabels.add(chessBoardSquares[jj][ii]);
        }
    }

    buttonsAndLabels.updateUI();

}

@Override
public void playGame(Interactable whiteUser, Interactable blackUser) {

        GraphicInterface gui = new GraphicInterface(buttonsAndLabels);
        gui.getChessBoard();


}

public JComponent getChessBoard() {
    return buttonsAndLabels;
}

public JComponent getGui() {
    return buttonsAndLabels;
}

@Override
public void mouseClicked(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

不要使用带有GUI的扫描仪。使用JOptionPane提示输入。我看不到类
板的代码。我假设您正在尝试将[国际象棋]棋子放在
棋盘上。你也应该阅读。您的问题是将棋子放在
棋盘上
,因此请尝试编写这样做的代码,即将棋子放在
棋盘上
,然后发布该代码,而不是整个棋局。