Home » Android » java – draw dice and face value using onDraw?-Exceptionshub

java – draw dice and face value using onDraw?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have an assignment to make a dice roll game but instead of images i need to use the onDraw method to do this for the result And I really cant find much of any info on how to achieve this.so first I am trying to achieve the die. What I have so far looks more like a dominoe. There has to be a better way any ideas? And please be gentle I am new to this but I really am trying it’s not easy at 46 but I want to learn. Here is what I have so far.

package com.example.circle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.nio.file.Path;

public class MyView extends View {

    private final Paint paint = new Paint();
    // --Commented out by Inspection (2020-02-16 17:11):Path path;


    public MyView(Context context) {
        super(context);
        init();
    }

    public  MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public MyView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    private void init() {
        paint.setColor( Color.LTGRAY );
        paint.setStrokeWidth( 10 );
        paint.setStyle( Paint.Style.STROKE );

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        //The number 1
        super.onDraw(canvas);
        paint.setStyle( Paint.Style.STROKE );
        canvas.drawRect(
                getLeft()+(getRight()-getLeft())/3,
                getTop()+(getBottom()-getTop())/3,
                getRight()-(getRight()-getLeft())/3,
                getBottom()-(getBottom()-getTop())/3,paint);
        super.onDraw(canvas);

        paint.setStyle( Paint.Style.STROKE );
        canvas.drawCircle( 620,800,30, paint );

        paint.setStyle( Paint.Style.STROKE );
        canvas.drawCircle( 620,870,30, paint );

        paint.setStyle( Paint.Style.STROKE );
        canvas.drawCircle( 620,940,30, paint );

        paint.setStyle( Paint.Style.STROKE );
        canvas.drawCircle( 500,940,30, paint );

        paint.setStyle( Paint.Style.STROKE );
        canvas.drawCircle( 500,800,30, paint );

        paint.setStyle( Paint.Style.STROKE );
        canvas.drawCircle( 500,870,30, paint );
}   }
How to&Answers: