Question

I am developing game in android. I am beginner so I don't know much about android. I was facing problem related to canvas. I am trying to design board which contains grid of circles(5*6). I want to show lines between them so that they look like boxes. For that I use drawLine() function. I tried different combination of values but still I am unable to show canvas as required. I want to remove lines which are going upwards from first row of circles and from left most column of circles. I am sharing my code below, please check out this code and suggest me solution? This link shows output which I am currently getting on my device's screen (http://postimg.org/image/m4kgv4ezz/).

Code:

package com.example.linedraw;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new board(this));
    }

    public class board extends View
    {
        Paint pBack = new Paint();
        Paint pDot = new Paint();

        int cols = 5;
        int rows = 6;

        public board(Context context)
        {
            super(context);
            pBack.setARGB(255, 255, 102, 0);
            pDot.setARGB(255, 255, 255, 255);
        }

        @SuppressLint("DrawAllocation")
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
            canvas.drawPaint(pBack);
           // BitmapFactory.decodeResource(getResources(), R.drawable.marbleback);
           //Bitmap bd= BitmapFactory.decodeResource(getResources(), R.drawable.blackball);
            float xStep = canvas.getWidth() / (cols + 1);
            float yStep = canvas.getHeight() / (rows + 1);

for (int y = 0; y < rows; y++)
        {
            for (int x = 0; x < cols; x++)
            {
                canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot);
                canvas.drawLine((x + 1) * xStep, (y + 1) * yStep, (rows - 1) * (2+xStep), (y+1) * (yStep), pDot);
                //canvas.drawLine((x -1) * xStep, (y -1) * yStep, (x-1) * xStep, (y+1) * yStep, pDot);
                canvas.drawLine((x - 1) * xStep, (y +1) * yStep, (x-1) * (xStep), (cols+1) * (1+yStep), pDot);
               // canvas.drawLine(startX, startY, stopX, stopY, paint)
              // canvas.drawBitmap(bd,canvas.getHeight()/(rows+1), canvas.getWidth()/(cols+1), pDot);
            }
        }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}
Was it helpful?

Solution

I still had this project saved. Just a few additions to the for loops:

for (int y = 0; y < rows; y++)
{
    for (int x = 0; x < cols; x++)
    {
        canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot);

        if (y == 0)
        {
            canvas.drawLine((x + 1) * xStep, yStep, (x + 1) * xStep, rows * yStep, pDot);
        }                   
    }

    canvas.drawLine(xStep, (y + 1) * yStep, cols * xStep, (y + 1) * yStep, pDot);
}

OTHER TIPS

Try below code :
for (int y = 0; y < rows; y++)
            {
                for (int x = 0; x < cols; x++)
                {
                    canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot);

                    canvas.drawLine( xStep, yStep, xStep, (cols) * (1+yStep), pDot);

                }
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top