Desenhar tabuleiro (8x8) no Canvas [RESOLVIDO]

Olá pessoal,

Estou tentando desenhar um tabuleiro (8x8) no android.graphics.Canvas com uma lógica que utilizo para desenhar no java.awt.Graphics2D (J2SE e funciona perfeitamente), porém, no Canvas do Android, o desenho do tabuleiro fica estranho (em anexo).

Segue o código:

View

package hfs.view.chessboard;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class ChessBoardView extends View {
	private short chessBoardSize;
	private short chessHouseSize;
	private Paint chessPaint = new Paint();

	public ChessBoardView(Context context) {
		super(context);
		init();
	}

	public ChessBoardView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ChessBoardView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		this.chessBoardSize = 160;
		this.chessHouseSize = (short) (chessBoardSize / 8);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.save();
		boolean color = true;
		for (short i = 0; i < chessBoardSize; i += chessHouseSize) {
			color = !color;
			for (short j = 0; j < chessBoardSize; j += chessHouseSize) {
				color = !color;
				if (color == true) {
					chessPaint.setColor(Color.BLUE);
				} else {
					chessPaint.setColor(Color.RED);
				}
				canvas.drawRect(i, j, chessHouseSize, chessHouseSize,
						chessPaint);
			}
		}
		canvas.restore();
	}
}

Activity

[code]
package hfs.main.chessset;

import hfs.view.chessboard.ChessBoardView;
import android.app.Activity;
import android.os.Bundle;

public class ChessSetActivity extends Activity {
private ChessBoardView chessBoardView;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.chessBoardView = new ChessBoardView(this);
	setContentView(chessBoardView);
}

}[/code]

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <hfs.view.chessboard.ChessBoardView
        android:id="@+id/chessBoardView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </hfs.view.chessboard.ChessBoardView>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hfs.main.chessset"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ChessSetActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Já tentei usar outros Layout Manager no main.xml, mas nenhum deu certo.

Alguém tem alguma sugestão?

Desde já agradeço pela atenção.

O problema é em:

canvas.drawRect(i, j, chessHouseSize, chessHouseSize, chessPaint);  

O método Canvas.drawRect recebe (left, top, right, bottom) , diferente do Graphics2D que recebe (left, top, width, height), então o correto seria:

canvas.drawRect(i, j, i + chessHouseSize,  j +  chessHouseSize, chessPaint);  

Muito Obrigado Marky.Vasconcelos :smiley:

Funcionou :thumbup:

O maior problema era que eu estava associando right <=> width e bottom <=> heigth.

vlw