You are on page 1of 21

GameView.

java
public class GameView extends View { private private private private private private final LineActivity lineActivity; float width; // width of one tile float height; // height of one tile int selX; // X index of selection int selY; // Y index of selection final Rect selRect = new Rect();

public GameView(LineActivity LineActivity) { super(LineActivity); this.lineActivity = LineActivity; setFocusable(true); setFocusableInTouchMode(true); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { width = w / 9f; height = h / 9f; getRect(selX, selY, selRect); super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { Paint background = new Paint(); background.setColor(getResources().getColor(R.color.puzzle_background)); canvas.drawRect(0, 0, getWidth(), getHeight(), background); } private void getRect(int x, int y, Rect rect) { rect.set((int) (x * width), (int) (y * height), (int) (x * width + width), (int) (y * height + height)); } }

GameManager.java public class GameManager { }

color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="line_background">#ffe6f0ff</color> </resources>

LineActivity.java public class LineActivity extends Activity {


private GameView gameView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); gameView = new GameView(this); setContentView(gameView); gameView.requestFocus(); } }

GameView.java @Override protected void onDraw(Canvas canvas) {


Paint background = new Paint(); background.setColor(getResources().getColor(R.color.line_background)); canvas.drawRect(0, 0, getWidth(), getHeight(), background); // Draw the board... // Define colors for the grid lines Paint dark = new Paint(); dark.setColor(getResources().getColor(R.color.line_dark)); Paint hilite = new Paint(); hilite.setColor(getResources().getColor(R.color.line_hilite)); Paint light = new Paint(); light.setColor(getResources().getColor(R.color.line_light)); // Draw the minor grid lines for (int i = 0; i < 9; i++) { canvas.drawLine(0, i * height, getWidth(), i * height,light); canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite); canvas.drawLine(i * width, 0, i * width, getHeight(),light); canvas.drawLine(i * width + 1, 0, i * width + 1,getHeight(), hilite); }

color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="line_background">#ffe6f0ff</color> <color name="line_hilite">#ffffffff</color> <color name="line_light">#64c6d4ef</color> <color name="line_dark">#6456648f</color> </resources>

GameView.java @Override protected void onDraw(Canvas canvas) {


Paint background = new Paint(); background.setColor(getResources().getColor(R.color.line_background)); canvas.drawRect(0, 0, getWidth(), getHeight(), background); // Draw the board... // Define colors for the grid lines Paint dark = new Paint(); dark.setColor(getResources().getColor(R.color.line_dark)); Paint hilite = new Paint(); hilite.setColor(getResources().getColor(R.color.line_hilite)); Paint light = new Paint(); light.setColor(getResources().getColor(R.color.line_light)); // Draw the minor grid lines for (int i = 0; i < 9; i++) { canvas.drawLine(0, i * height, getWidth(), i * height,light); canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite); canvas.drawLine(i * width, 0, i * width, getHeight(),light); canvas.drawLine(i * width + 1, 0, i * width + 1,getHeight(), hilite); }

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ball1), 10, 10, null);


}

GameView.java
public class GameView extends View { private private private private private private final LineActivity lineActivity; float width; // width of one tile float height; // height of one tile int selX; // X index of selection int selY; // Y index of selection final Rect selRect = new Rect();

public GameView(LineActivity LineActivity) { super(LineActivity); this.lineActivity = LineActivity; setFocusable(true); setFocusableInTouchMode(true); }

@Override public boolean onTouchEvent(MotionEvent e) { if(e.getAction() == MotionEvent.ACTION_DOWN) { float touchX = e.getX(); float touchY = e.getY(); Point pos = getPosition(touchX,touchY); String mess = Integer.toString(pos.x) + "-" + Integer.toString(pos.y); Toast.makeText(lineActivity, mess , Toast.LENGTH_SHORT).show(); } return super.onTouchEvent(e); } private Point getPosition(float x, float y) int row = 0; int col = 0; //... return new Point(row,col); }
}

GameView.java
public class GameView extends View { private final LineActivity lineActivity; private float width; // width of one tile private float height; // height of one tile private int selX; // X index of selection private int selY; // Y index of selection private final Rect selRect = new Rect(); private Timer timer = new Timer(); private Bitmap[] bmps = new Bitmap[7]; public GameView(LineActivity LineActivity) { super(LineActivity); this.lineActivity = LineActivity; setFocusable(true); setFocusableInTouchMode(true); bmps[0] = BitmapFactory.decodeResource(getResources(), bmps[1] = BitmapFactory.decodeResource(getResources(), bmps[2] = BitmapFactory.decodeResource(getResources(), bmps[3] = BitmapFactory.decodeResource(getResources(), bmps[4] = BitmapFactory.decodeResource(getResources(), bmps[5] = BitmapFactory.decodeResource(getResources(), bmps[6] = BitmapFactory.decodeResource(getResources(), timer.schedule(new TimerTask() { @Override public void run() { lineActivity.runOnUiThread(Timer_Tick); } }, 0, 1000); } int count = 0; private Runnable Timer_Tick = new Runnable() { public void run() { count++; if (count == 7) count =0; } }; }

R.drawable.ball1); R.drawable.ball2); R.drawable.ball3); R.drawable.ball4); R.drawable.ball5); R.drawable.ball6); R.drawable.ball7);

ImageUtils.java
public class ImageUtils{ public static Bitmap resizeBitmap(Bitmap bitmap,int newWidth, int newHeight){ int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleWidth = ((float)newWidth)/width; float scaleHeight = ((float)newHeight)/height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap bit = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); return bit; } }

GameView.java @Override protected void onDraw(Canvas canvas) {


Paint background = new Paint(); background.setColor(getResources().getColor(R.color.line_background)); canvas.drawRect(0, 0, getWidth(), getHeight(), background); // Draw the board... // Define colors for the grid lines Paint dark = new Paint(); dark.setColor(getResources().getColor(R.color.line_dark)); Paint hilite = new Paint(); hilite.setColor(getResources().getColor(R.color.line_hilite)); Paint light = new Paint(); light.setColor(getResources().getColor(R.color.line_light)); // Draw the minor grid lines for (int i = 0; i < 9; i++) { canvas.drawLine(0, i * height, getWidth(), i * height,light); canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite); canvas.drawLine(i * width, 0, i * width, getHeight(),light); canvas.drawLine(i * width + 1, 0, i * width + 1,getHeight(), hilite); }

Bitmap bmpLarge = ImageUtils.resizeBitmap(bmps[count],200,200); canvas.drawBitmap(bmpLarge, 50, 10, null); Bitmap bmpSmall = ImageUtils.resizeBitmap(bmps[count],20,20); canvas.drawBitmap(bmpSmall, 100, 300, null);
}

Ball.java
public class Ball { private int mRowIndex = -1; private int mColumnIndex = -1; private Bitmap bitmap=null; public Ball(){ } }

GameManager.java
public class GameManager { private GameView gameView; private Ball [][]balls; public static final int LINE_SIZE = 9; public GameManager(GameView gameview){ this.gameView = gameview; } }

GameView.java
public class GameView { private Ball getBallAtPoint(int x, int y) { int lx = x - getPaddingLeft(); int ly = y - getPaddingTop(); int row = (int) (ly / mCellHeight); int col = (int) (lx / mCellWidth); if(col >= 0 && col < GameManager.SUDOKU_SIZE && row >= 0 && row < GameManager.SUDOKU_SIZE) { return gameManager.getBall[row][col]; } else { return null; } } }

You might also like