国际象棋,游戏板翻了吗?

时间:2013-12-14 20:17:17

标签: java swing awt

我正在尝试用Java创建游戏板(国际象棋)的初始状态。但是,当我运行程序时,电路板似乎被翻转了?如果你看一下初始状态方法,这就是我希望我的初始板开始的方式:

public void initialBoardState(){
    gameBoard = new int [][] 
            {{22,23,24,25,26,24,23,22},
            {21,21,21,21,21,21,21,21}, //Black pieces
            {0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0},     
            {0,0,0,0,0,0,0,0},     
            {0,0,0,0,0,0,0,0},     
            {11,11,11,11,11,11,11,11}, //White pieces
            {12,13,14,15,16,14,13,12}};

 }

然而,当我运行它时,电路板似乎会像这样翻转:

gameBoard= new int[][]
      {{22,21,0,0,0,0,11,12},
      {23,21,0,0,0,11,13}

等等..有人可以解释一下吗?整个班级发布在下面:

    import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;


/**
 * @author Cian
 * This class will draw our game board
 */

class Board extends JComponent implements MouseListener{

    private int gameBoard[][] = new int[8][8];
    public int row = 8;
    public int column = 8;
    public int cell = 80;
    public int rowSelected;
    public int colSelected;
    public int currentPlayer = 1;
    int x; int y; int a; int b;

    public Board(){
        addMouseListener(this);
        initialBoardState();
    }

    public void paintComponent(Graphics g){
        super.paintComponents(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, 640, 640);
        drawBoard(g2d);
    }

    public void drawBoard(Graphics2D g2d){
        g2d.setColor(Color.black);

        // draw the edge lines (0,0,0,640), (0,0,640,0), (0,640,640,640), (640,0,640,640)
        g2d.drawLine(0, 0, 0, 639);
        g2d.drawLine(0, 0, 639, 0);
        g2d.drawLine(0, 639, 639, 639);
        g2d.drawLine(639, 0, 639, 639);

        // draw the horizontal lines using a loop from one to 7, coordiates of each line is (0, x*80, 640, x*80) also
        // draw vertical lines with coordinates of (x*80, 0, x*80, 640)
        for(int i = 1; i < 8; i++) {
            g2d.drawLine(0, i*80, 640, i*80);
            g2d.drawLine(i*80, 0, i*80, 640);
        }

        for ( int row = 0; row < 8; ++row )  // ++ means "increment by one" 
            // This inner loop counts over all the (white or black) squares in a row. It starts at 0 if row is even, or 1 if row is odd, and goes up to no more than 7 (so it will count 1-3-5-7 or 0-2-4-6 
            for ( int i = row % 2; i <= 7; i+= 2 ) {  // += means "add to that variable
                g2d.setColor(Color.BLACK);
                g2d.fillRect ( getX() + cell*i, getY() + row*cell, cell, cell); 
            }

        BufferedImage img_black = null;
        BufferedImage castle_black = null;
        BufferedImage castle_white = null;
        BufferedImage img_white = null;
        try {
            img_black = ImageIO.read(new File("imgs/pawn-black.png"));
            castle_black = ImageIO.read(new File("imgs/castle-black.png"));
            castle_white = ImageIO.read(new File("imgs/castle-white.png"));
            img_white = ImageIO.read(new File("imgs/pawn-white.png"));
            } catch (IOException e) {
        }

        for(int a=0;a<8;a++){
            for(int b=0;b<8;b++){
                if(gameBoard[a][b] == 22){
                    g2d.drawImage(castle_black,a*80, b*80,null);
                }
                if(gameBoard[a][b] == 12){
                    g2d.drawImage(castle_white,a*80, b*80,null);
                }
                if(gameBoard[a][b] == 0){
                    g2d.drawImage(castle_white,a*80, b*80,null);
                }

            }

        }

        /*for(int i=0;i<8;i++){
            for(int j=1;j<2;j++){
                g2d.drawImage(img_black,i*80, j*80,null);
            }
        }


        for(int i=0;i<8;i++){
            for(int j=6;j<7;j++){
                g2d.drawImage(img_white,i*80, j*80,null);
            }
        }*/
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }


    public void mousePressed(MouseEvent event) {
        x = event.getX(); y = event.getY();
        rowSelected = x / cell;
        colSelected = y / cell;
        if (rowSelected >= 0 && rowSelected < row && colSelected >= 0 && colSelected < column){
            System.out.println("X: "+rowSelected+" | Y: "+colSelected+"  |  GameBoard: "+gameBoard[rowSelected][colSelected]);
        }
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        a = event.getX();                               //a = mouse released x coordinates
        b = event.getY();                               //b = mouse released y coordinates
        //isValidMove(x,y,a,b);
    }

    /*private void isValidMove(int x, int y, int a, int b){
        if(gameBoard[x][y]/10 == currentPlayer){
                int piece = gameBoard[x][y]%10;
                if(piece == 1){
                    System.out.println("1");   
                }
        }
}*/

    public void initialBoardState(){
        gameBoard = new int [][] 
                {{22,23,24,25,26,24,23,22},
                {21,21,21,21,21,21,21,21}, //Black pieces
                {0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0},     
                {0,0,0,0,0,0,0,0},     
                {0,0,0,0,0,0,0,0},     
                {11,11,11,11,11,11,11,11}, //White pieces
                {12,13,14,15,16,14,13,12}};

     }

}

1 个答案:

答案 0 :(得分:1)

从描述问题的方式来看,您似乎只是输入了 x 坐标,您应该输入 y 坐标,反之亦然。所以这只是一个疯狂的猜测,但尝试更改类似的行:

g2d.drawImage(castle_black,a*80, b*80,null);

对此:

g2d.drawImage(castle_black,b*80, a*80,null);

此外,根据this,该方法被声明为public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y),所以对我来说,似乎你没有很好地使用它,它应该是这样的:

g2d.drawImage(castle_black, null, b*80, a*80);

但我可能错了,因为我没有经验。