Translate

Friday, 29 April 2016

SudokuCell.java

package com.vikesh.gamesudoku.view.sudokugrid;

import com.vikesh.gamesudoku.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.Log;
//import android.util.AttributeSet;
//import android.view.View;

public class SudokuCell extends BaseSudokuCell {

private Paint mPaint;
public int i=0;
//private int number;
public SudokuCell(Context context) {
super(context);
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(1);
mPaint.setStyle(Style.STROKE);
Paint background = new Paint();
   background.setColor(getResources().getColor(
           R.color.puzzle_background));
   canvas.drawRect(0, 0, getWidth(), getHeight(), background);
   drawNumbers(canvas);
   drawLines(canvas);
}

private void drawNumbers(Canvas canvas)
{
mPaint.setColor(getResources().getColor(
          R.color.puzzle_foreground));
mPaint.setTextSize(20);

mPaint.setStyle(Style.FILL);
Rect bounds = new Rect();
mPaint.getTextBounds(String.valueOf(getValue()),0,String.valueOf(getValue()).length(), bounds);
if(getValue()!=0)
{
canvas.drawText(String.valueOf(getValue()),(getWidth()-bounds.width())/2,(getWidth()+bounds.width())/2, mPaint);

}
else
{
canvas.drawColor(Color.LTGRAY);
}

}

private void drawLines(Canvas canvas)
{
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(1);
mPaint.setStyle(Style.STROKE);
Paint hilite = new Paint();
   hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
Paint dark = new Paint();
   dark.setColor(getResources().getColor(R.color.puzzle_dark));
   for(i=0;i<9;i++)
     {
     if(i%3!=0)
     continue;
     canvas.drawLine(0,i*getHeight(), getWidth(), i*getHeight(),dark);
     canvas.drawLine(0,i*getHeight()+1, getWidth(), i*getHeight()+1, hilite);
     canvas.drawLine(i*getWidth(),0,i* getWidth(), getHeight(),dark);
     canvas.drawLine(i*getWidth()+1,0, i*getWidth()+1, getHeight(),hilite);
     }
}

}

No comments:

Post a Comment