0

我一直在尝试使用 java 绘制板,但每次运行时都会出现错误并且代码停止运行。这是导致问题的最后一行代码,但我不明白为什么。

protected void onDraw(Canvas canvas) {
    //Draw the background...
    Paint background = new Paint();
    background.setColor(getResources().getColor(R.color.game_background));
    canvas.drawRect(0, 0, getWidth(), getHeight(), background);

    //Draw the board
    //Draw the selection


//Draw the board....
//Define colours for grid lines
Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.game_hilite));
Paint light = new Paint();
light.setColor(getResources().getColor(R.color.game_light));
Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.game_dark));
Paint tile1 = new Paint();
dark.setColor(getResources().getColor(R.color.Tile_1));



//Draw the minor grid lines

for (int i = 0; i <11; i++) {
    canvas.drawLine(0, i * height, getWidth(), i * height, dark);
    canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, dark);
    canvas.drawLine(i * width, 0, i * width, getHeight(), light);
    canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), dark);    
}
//Draw the major grid lines
for (int i = 0; i < 11; i++) {
    if (i % 3 != 0)
        continue;
    canvas.drawLine(0, i * height, getWidth(), i * height, dark);
    canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, dark);
    canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
    canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), dark);
}

//Draw the numbers .... 
//Define color and style for numbers 
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
foreground.setColor(getResources().getColor(R.color.game_foreground));
foreground.setStyle(Style.FILL);
foreground.setTextSize(height * 0.75f);
foreground.setTextScaleX(width / height);
foreground.setTextAlign(Paint.Align.CENTER);

//Draw the number in the center of the tile
FontMetrics fm = foreground.getFontMetrics();
//Centering in X: use alignment (and X at midpoint)
float x = width / 2;
//Centering in Y: measure ascent/decent first
float y = height / 2 - (fm.ascent + fm.descent) / 2; 
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            canvas.drawText(this.game.getTileString(i,j), i * width + x, j * height + y, foreground);
        }
    }

}
4

0 回答 0