Android绘图棋子

时间:2016-04-14 18:49:05

标签: java android bitmap draw

我目前正在开发一个Android版本的国际象棋,但我遇到了绘制问题。我已经设置了一个ChessboardView类,它扩展了视图并使用了另一个名为Tile的帮助器类。问题是我的所有作品都被绘制在正确的位置(我的调试器显示当我点击瓷砖时我正在击中的那块)但是所有的图像都显示为黑色皇后(我认为因为它是最后一个案例,在我的开关语句中被击中,因为当我摆脱它时,所有的碎片都变成了黑色的车。以下是相关代码:

ChessboardView.java:

protected void onDraw(final Canvas canvas){
    final int width = getWidth();
    final int height = getHeight();
    this.squareSize = Math.min(
            getSquareSizeWidth(width),
            getSquareSizeHeight(height)
    );
    getOrigins(width,height);
    for(int col = 0; col < NUM_RC; col++){
        for(int row = 0; row < NUM_RC; row++){
            final int xCoord = getXCoord(col);
            final int yCoord = getYCoord(row);
            final Rect tileRect = new Rect(xCoord,yCoord,xCoord+squareSize,yCoord+squareSize);
            String loc = getRowString(row) + getColumnString(col);
            String piece = Chessboard.getPieceAtLocation(loc);
            tiles[col][row].setTileRect(tileRect);
            tiles[col][row].draw(canvas,piece,getContext());
        }
    }
}

Tile.java

public void draw(Canvas canvas,String piece, Context c){
    canvas.drawRect(tileRect, tileColor);
    if(!piece.equals("  ") && !piece.equals("##")){
        Bitmap pieceBM = null;
        switch(piece) {
            case "wp": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_pawn);
            }
            case "wK": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_king);
            }
            case "wB": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_bishop);
            }
            case "wN": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_knight);
            }
            case "wR": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_rook);
            }
            case "wQ": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.white_queen);
            }
            case "bp": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_pawn);
            }
            case "bK": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_king);
            }
            case "bB": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_bishop);
            }
            case "bN": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_knight);
            }
            case "bR": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_rook);
            }
            case "bQ": {
                pieceBM = BitmapFactory.decodeResource(c.getResources(), R.drawable.black_queen);
            }
        }
        canvas.drawBitmap(pieceBM,null,tileRect,null);
    }
}

1 个答案:

答案 0 :(得分:3)

您需要在每个案例的末尾添加break;。否则它会掉下来并执行下一个案例。