Home>

I want to draw a new circle when I touch a circle, but it seems that an error has occurred around onTouch ().
I cannot figure it out on my own.

public class Circles extends View implements View.OnTouchListener {
    final Paint paint;
    static int r = 60;
    static int i = 0;
    static int x;
    static int y;
    final static int bc = Color.BLACK;
    final static int dc = Color.MAGENTA;
    public Circles (Context context, AttributeSet attrs) {
        super (context, attrs);
        paint = new Paint ();
    }
    @Override
    protected void onDraw (Canvas canvas) {
        super.onDraw (canvas);
        final int width = canvas.getWidth ();
        final int height = canvas.getHeight ();
        x = (int) (Math.random () * width);
        y = (int) (Math.random () * height);
        if ((x<width-2 * r)&&(y<height-2 * r)) {
            canvas.drawColor (bc);
            paint.setColor (dc);
            canvas.drawCircle (x, y, r, paint);
            findViewById (R.id.view) .setOnTouchListener (this);
        } else {
            invalidate ();
        }
    }
    protected void onTouch (View v, MotionEvent e) {
        double mouseX = e.getX ();
        double mouseY = e.getY ();
        if (mouseX>x&&mouseX<x + 2 * r) {
            if (mouseY>y&&mouseY<y + 2 * r) {
                invalidate ();
            }
        }
    }
}