Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameBoard extends View {
- private static final String TAG = GameBoard.class.getSimpleName();
- private static final int ROWS = 10;
- private static final int COLS = 10;
- private final Tile[][] mTiles;
- private int squareSize = 0;
- private int x0 = 0;
- private int y0 = 0;
- public GameBoard(Context context) {
- super(context);
- this.mTiles = new Tile[COLS][ROWS];
- setFocusable(true);
- buildTiles();
- }
- private void buildTiles() {
- for (int col = 0; col < COLS; ++col) {
- for (int row = 0; row < ROWS; ++row) {
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.raw.battleshiptile);
- mTiles[col][row] = new Tile(col, row, bitmap);
- }
- }
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // super.onDraw(canvas);
- final int height = getHeight();
- final int width = getWidth();
- this.squareSize = Math.min(getSquareSizeHeight(height), getSquareSizeWidth(width));
- calculateInitialCoord(height, width);
- for (int col = 0; col < COLS; ++col) {
- for (int row = 0; row < ROWS; ++row) {
- final int xCoord = getXCoordination(col);
- final int yCoord = getYCoordination(row);
- final Rect tileSquare = new Rect(xCoord, yCoord, xCoord + this.squareSize, yCoord + this.squareSize);
- mTiles[col][row].setTileSquare(tileSquare);
- mTiles[col][row].draw(canvas);
- }
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // return super.onTouchEvent(event);
- final int x = (int) event.getX();
- final int y = (int) event.getY();
- Tile tile;
- for (int i = 0; i < COLS; ++i) {
- for (int j = 0; j < ROWS; ++j) {
- tile = mTiles[i][j];
- if (tile.isTouched(x, y)) {
- tile.handleTouch();
- }
- }
- }
- return true;
- }
- private void calculateInitialCoord(int height, int width) {
- this.x0 = 0/*(width - squareSize * COLS) / 2*/;
- this.y0 = 0/*(height - squareSize * ROWS) / 2*/;
- }
- private int getXCoordination(final int x) {
- return this.x0 + this.squareSize * x;
- }
- private int getYCoordination(final int y) {
- return this.y0 + this.squareSize * y;
- }
- private int getSquareSizeWidth(int width) {
- return width / ROWS;
- }
- private int getSquareSizeHeight(int height) {
- return height / COLS;
- }
- this.layout = (RelativeLayout) findViewById(R.id.playLayout);
- RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
- GameBoard gb = new GameBoard(this);
- gb.setId(R.id.gameboard);
- layout.addView(gb, params);
- RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
- TextView tv = new TextView(this);
- tv.setText("Ships");
- params2.addRule(RelativeLayout.LEFT_OF, R.id.gameboard);
- layout.addView(tv, params2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement