Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. public class GameBoard extends View {
  2. private static final String TAG = GameBoard.class.getSimpleName();
  3.  
  4. private static final int ROWS = 10;
  5. private static final int COLS = 10;
  6. private final Tile[][] mTiles;
  7. private int squareSize = 0;
  8. private int x0 = 0;
  9. private int y0 = 0;
  10.  
  11. public GameBoard(Context context) {
  12. super(context);
  13. this.mTiles = new Tile[COLS][ROWS];
  14.  
  15. setFocusable(true);
  16. buildTiles();
  17. }
  18.  
  19. private void buildTiles() {
  20. for (int col = 0; col < COLS; ++col) {
  21. for (int row = 0; row < ROWS; ++row) {
  22. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.raw.battleshiptile);
  23. mTiles[col][row] = new Tile(col, row, bitmap);
  24. }
  25. }
  26. }
  27.  
  28. @Override
  29. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  30. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  31. }
  32.  
  33. @Override
  34. protected void onDraw(Canvas canvas) {
  35. // super.onDraw(canvas);
  36. final int height = getHeight();
  37. final int width = getWidth();
  38. this.squareSize = Math.min(getSquareSizeHeight(height), getSquareSizeWidth(width));
  39.  
  40. calculateInitialCoord(height, width);
  41.  
  42. for (int col = 0; col < COLS; ++col) {
  43. for (int row = 0; row < ROWS; ++row) {
  44. final int xCoord = getXCoordination(col);
  45. final int yCoord = getYCoordination(row);
  46. final Rect tileSquare = new Rect(xCoord, yCoord, xCoord + this.squareSize, yCoord + this.squareSize);
  47.  
  48. mTiles[col][row].setTileSquare(tileSquare);
  49. mTiles[col][row].draw(canvas);
  50. }
  51. }
  52. }
  53.  
  54. @Override
  55. public boolean onTouchEvent(MotionEvent event) {
  56. // return super.onTouchEvent(event);
  57. final int x = (int) event.getX();
  58. final int y = (int) event.getY();
  59.  
  60. Tile tile;
  61. for (int i = 0; i < COLS; ++i) {
  62. for (int j = 0; j < ROWS; ++j) {
  63. tile = mTiles[i][j];
  64. if (tile.isTouched(x, y)) {
  65. tile.handleTouch();
  66. }
  67. }
  68. }
  69. return true;
  70. }
  71.  
  72.  
  73. private void calculateInitialCoord(int height, int width) {
  74. this.x0 = 0/*(width - squareSize * COLS) / 2*/;
  75. this.y0 = 0/*(height - squareSize * ROWS) / 2*/;
  76. }
  77.  
  78. private int getXCoordination(final int x) {
  79. return this.x0 + this.squareSize * x;
  80. }
  81.  
  82. private int getYCoordination(final int y) {
  83. return this.y0 + this.squareSize * y;
  84. }
  85.  
  86. private int getSquareSizeWidth(int width) {
  87. return width / ROWS;
  88. }
  89.  
  90. private int getSquareSizeHeight(int height) {
  91. return height / COLS;
  92. }
  93.  
  94. this.layout = (RelativeLayout) findViewById(R.id.playLayout);
  95. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  96. GameBoard gb = new GameBoard(this);
  97. gb.setId(R.id.gameboard);
  98. layout.addView(gb, params);
  99.  
  100. RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
  101. TextView tv = new TextView(this);
  102. tv.setText("Ships");
  103. params2.addRule(RelativeLayout.LEFT_OF, R.id.gameboard);
  104. layout.addView(tv, params2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement