问题描述:

I am currently working on developing an android version of chess, but I am having an issue with draw. I have set up a ChessboardView class that extends view and uses another helper class called Tile. The problem is that all of my pieces are being drawn in the correct places (my debugger shows when I click on tiles what piece I am hitting) but all of the images are showing up as Black Queens (I think because it's the last case that is hit in my switch statement, because when I get rid of it all of the pieces turn to black rooks). Here is relevant code:

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);

}

}

网友答案:

You need to put a break; at the end of each case. Otherwise it falls down and executes the next case as well.

相关阅读:
Top