我有一个以前的程序,它绘制了一个带有设置分离值的矩形网格,我现在尝试更改此代码以允许我以完全相同的方式绘制正方形网格,我想这会这是一个简单的改变,但到目前为止它已经是一切了!我已经包含了我在下面尝试的内容,我试图改变所有的参数和计算,但无论我做什么,它似乎只包括一个矩形而不是填充板,就像我使用圆圈时一样。有没有人知道为什么会这样?
public class GameView extends View {
private Game mGame = new Game(Game.DEFAULT_COLUMNS,
Game.DEFAULT_ROWS);
private GestureDetector mGestureDetector;
private Paint mBGPaint;
private Paint mPlayer2Paint;
private Paint mGridPaint;
private Paint mPlayer1Paint;
double SEPATRATOR_RATIO;
int rowcount;
int columncount;
private float diameterX;
private float diameterY;
private float diameter;
private float separator;
public float availableWidth;
public float availableHeight;
private int tokenAtPos;
public GameView(Context context) {
super(context);
init();
// mGestureDetector = new GestureDetector(context, new
MyGestureListener());
}
private void init() {
mGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mGridPaint.setStyle(Paint.Style.FILL);
mGridPaint.setColor(0xff0000ff);
mPlayer1Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPlayer1Paint.setStyle(Paint.Style.FILL);
mPlayer1Paint.setColor(0xffff0000);
mPlayer2Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPlayer2Paint.setStyle(Paint.Style.FILL);
mPlayer2Paint.setColor(0xffffff00);
mBGPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBGPaint.setStyle(Paint.Style.FILL);
mBGPaint.setColor(0xffffffff);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
double SEPARATOR_RATIO=5;
int rowcount = mGame.getmRows();
int columncount = mGame.getmColumns();
double diameterX = floor(getWidth()/(columncount+(columncount+1)*SEPARATOR_RATIO));
double diameterY = ceil(getHeight()/(columncount+(columncount+1)*SEPARATOR_RATIO));
double diameter = (Math.min(diameterX, diameterY));
float separator = (float) (diameter * SEPARATOR_RATIO);
double availableWidth = (columncount+1) * diameter * SEPARATOR_RATIO
+ columncount * diameterX;
double availableHeight = (columncount+1) * diameter * SEPARATOR_RATIO
+ columncount * diameterX;
canvas.drawRect(0,0,(float)availableWidth, (float)availableHeight,mGridPaint);
for (int col=0;col<mGame.getmColumns(); col++)
{
for(int row=0; row<mGame.getmRows(); row++)
{
Paint paint;
// tokenAtPos = mGame.getToken(col,row);
// if (tokenAtPos==1)
// { paint=mPlayer1Paint; }
// else if (tokenAtPos==2)
// { paint=mPlayer2Paint; }
// else
{ paint=mBGPaint; }
float cx = (float)(separator+(diameter+separator)*col+diameter/2);
float cy = (float) (separator+(diameter+separator)*row+diameter/2);
// canvas.drawCircle (cx, cy, (float)diameter/2, paint);
canvas.drawRect (cx, cy, (float)diameterX, (float)diameterY, paint);
}
}
}
}