TTTBoard.java file

mail@pastecode.io avatar
unknown
java
a year ago
3.1 kB
1
Indexable
Never
package com.example.tiktaktoen3;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class TTTBoard extends View {

    private final int boardColor;
    private final int XColor;
    private final int OColor;
    private final int winningLineColor;

    // Paint object and 2 other methods (onDraw + drawGameBoard) are needed to draw our TTT Board
    private final Paint paint = new Paint();

    private int cellSize = getWidth()/3;

    public TTTBoard(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TTTBoard, 0, 0);

        // Extract values (and store them) of 4 attributes of TTTBoard
        try {
            boardColor = a.getInteger(R.styleable.TTTBoard_boardColor, 0);
            XColor = a.getInteger(R.styleable.TTTBoard_XColor, 0);
            OColor = a.getInteger(R.styleable.TTTBoard_OColor, 0);
            winningLineColor = a.getInteger(R.styleable.TTTBoard_winningLineColor, 0);
        } finally {
            a.recycle();
        }

    }

    // Overriding the onMeasure method, why? Prob. to adjust dimension of square TTTB to user's screen
    @Override
    protected void onMeasure(int width, int height) {
        //set up dimensions of TTT Board
        super.onMeasure(width, height);

        //adjustable TTT's dimensions based on user's screen
        int dimension = Math.min(getMeasuredWidth(), getMeasuredHeight()); // returns width|height of user's screen, usually width is smaller value
        cellSize = dimension/3;

        // define dimension of our TTT Board View
        setMeasuredDimension(dimension, dimension);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);

        drawGameBoard(canvas);

//        drawX(canvas,1,0);
//        drawO(canvas, 2,2);
    }



    private void drawGameBoard(Canvas canvas) {

        paint.setColor(boardColor);
        paint.setStrokeWidth(16);

        // need to know cell's size of TTT Board
        for (int c=1; c<3; c++) {
            canvas.drawLine(cellSize*c, 0, cellSize*c, canvas.getWidth(), paint);

        }

        for (int r=1; r<3; r++) {
            canvas.drawLine(0, cellSize*r, canvas.getWidth(), cellSize*r, paint);
        }
    }

    private void drawX(Canvas canvas, int row, int col) {
        paint.setColor(XColor);

        canvas.drawLine(cellSize*col, cellSize*row, cellSize*(col+1), cellSize*(row+1), paint);
        canvas.drawLine(cellSize*col, cellSize*(row+1), cellSize*(col+1), cellSize*row, paint);

    }

    private void drawO(Canvas canvas, int row, int col) {
        paint.setColor(OColor);

        canvas.drawOval(col*cellSize, row*cellSize, (col+1)*cellSize, (row+1)*cellSize, paint);
    }

}