0

I am trying to make game in android. I have drawn a board which contains grid of circles(5*6). I am using canvas, I want to draw line when user touch between two circles either horizontally or vertically. whenever a touch event occur it should take x,y coordinates of both circles and drawLine() between them. I have tried this but I am facing two problems as mentioned below.

  • When I run this code it shows line on upper left corner of device's screen. Problem is it is showing line without touching screen.
  • Second problem is that it should show line between circles and it should not allow line other then selected area.

Here is the code. Please suggest me solution.

package com.example.tap;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.gesture.Gesture;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
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 xpos=0;
        int ypos=0;

        int cols = 5;
        int rows = 6;

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

        @SuppressLint("DrawAllocation")
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
            canvas.drawPaint(pBack);
            float xStep = canvas.getWidth() / (cols + 1);
            float yStep = canvas.getHeight() / (rows + 1);
            float curCirclXpos, curCirclYpos, lastCirclXpos = 0, lastCirclYpos =0;
            //float vertical= 
//          boolean onTap(Gesture g, Point p)
//          {
//              
//          }

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

                    if (y == 0)
                    {
                        //canvas.drawLine((x + 1) * xStep, yStep, (x + 1) * xStep, rows * yStep, pDot);
                        //canvas.drawLine(xpos, ypos, xpos, yStep, pDot);
                        canvas.drawLine(curCirclXpos, curCirclYpos, lastCirclXpos, lastCirclYpos, pDot);
                    }                   
                }

                //canvas.drawLine(xStep, (y + 1) * yStep, cols * xStep, (y + 1) * yStep, pDot);
            }
        }
        public boolean onTouchEvent(MotionEvent e)
        {
            xpos=(int) e.getX();
           ypos=(int) e.getY();
            switch (e.getAction())
            {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_MOVE:
                //Log.d("Umar",String.valueOf(xpos));
                //Log.d("Farooq",String.valueOf(ypos));

            break;

            }
            return false;


        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

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


}
Umar Farooq
  • 127
  • 1
  • 2
  • 19
  • check this if it helps http://stackoverflow.com/questions/16650419/draw-in-canvas-by-finger-android – Raghunandan Apr 01 '14 at 06:48
  • @Raghunandan it is giving me insight, but it is not according to my needs, I want to display a line, I don't want to draw line manually instead I want to display line of specific length when user taps on screen. – Umar Farooq Apr 01 '14 at 08:39

0 Answers0