0

I am new to programing. I am trying to move a circle by using the OnTouchListener, but I don't know how to call it within the onTouch event as I have a Class for Circle with x and y and a class called Drawing (d) which draws it on the canvas. I have highlighted bellow the lines which I find problematic. Thank you.

``

public class FirstPicture extends Activity implements OnTouchListener {
    Drawing d;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            d=new Drawing(this);

            setContentView(d);
            d.setOnTouchListener(this);
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            **d.c1.getX() = event.getX();
            d.c1.getY()= event.getY();**
            d.invalidate();


            return true;
        }
    }


public class Drawing  extends View{
    Context ctx;
    Circle c1;

    public Drawing(Context context) {
            super(context);
            this.ctx = context;
            c1 = new Circle (165, 350, 33);


        }
    protected void onDraw (android.graphics.Canvas canvas){
        Paint p = new Paint();
        Paint p1 = new Paint ();
        Paint p2 = new Paint ();
        Paint p3= new Paint();
        Paint p4 = new Paint();

        p2.setColor(Color.WHITE);
        p.setColor(Color.GREEN);
        p3.setColor(Color.RED);
        p1.setColor(Color.BLACK);
        p4.setColor(Color.BLUE);
        p1.setStyle(Style.FILL);
        p1.setStyle(Style.FILL_AND_STROKE);
        p1.setTextSize(19);

        canvas.drawCircle(c1.getX(), c1.getY(), c1.getR(), p4);
        canvas.drawText("100", c1.getX()-15, c1.getY()+5, p1);

    }

    }

    public class Circle {
    private float x;
    private float y;
    private float r;
    public Circle(float x, float y, float r) {
        super();
        this.x = x;
        this.y = y;
        this.r = r;
    }
    public float getX() {
        return x;
    }
    public void setX(float x) {
        this.x = x;
    }
    public float getY() {
        return y;
    }
    public void setY(float y) {
        this.y = y;
    }
    public float getR() {
        return r;
    }
    public void setR(float r) {
        this.r = r;
    }

    }
salih kallai
  • 879
  • 2
  • 13
  • 34

1 Answers1

0

You're trying to assign to a getter:

d.c1.getX() = event.getX()

How about exposing an update method on your Drawing view.

private int circleCenterX;
private int circleCenterY;

public void update(int x, int y) {
    this.circleCenterX = x;
    this.circleCenterY = y;
}

@Override
protected void draw(Canvas canvas) {
    canvas.drawCircle(circleCenterX, circleCenterY, c1.getR(), p4);
    canvas.drawText("100", c1.getX() - 15, c1.getY() + 5, p1);
}

Then your onTouch becomes:

@Override
public boolean onTouch(View v, MotionEvent event) {
    d.update(event.getX(), event.getY());
    return true;
}
  • Instantiate all your Paint objects outside of the draw method - perhaps in your constructor where you instantiate the circle. Instantiating objects is a relatively heavy operation and you don't want to do it while draw() is happening, especially as it happens 60x a second.
  • I'm semi-sure the draw() method on the view will automatically be called so there's no need to invalidate the view (but even if you do, it won't redraw the view until the next frame anyway - take this bullet point and this bit inside parentheses with a pinch of salt!)
ataulm
  • 15,195
  • 7
  • 50
  • 92