0

I am using ready code from this.

in the code we have circles shapesArrayList<Circle> mCircles; the shapes are created in a custom view class. and drawn in the view see like the following

private void createCircles() {
    mCircles = new ArrayList<Circle>();
    mCircles.add(new Circle(30, 30, 20));
    mCircles.add(new Circle(120, 30, 20));
}
private void drawCirclesOnCanvas(Canvas canvas) {
    for (Circle c : mCircles) {
        canvas.drawCircle(c.getCurrentX(), c.getCurrentY(), c.getRadius(),
                mForeCirclePaint);
    }
}

in summary the handle touch function for the circle shape is

private void handleTouchedCircle(MotionEvent me, Circle c) {
    final float me_x = me.getX();
    final float me_y = me.getY();
    final int action = me.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        c.setActionDownX(c.getCurrentX());
        c.setActionDownY(c.getCurrentY());
        c.setActionMoveOffsetX(me_x);
        c.setActionMoveOffsetY(me_y);
        break;
    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_UP:
        c.setCurrentX(c.getActionDownX() + me_x - c.getActionMoveOffsetX());
        c.setCurrentY(c.getActionDownY() + me_y - c.getActionMoveOffsetY());
        break;
    case MotionEvent.ACTION_CANCEL:
        c.restoreStartPosition();
        break;
    }
}

this make the circle move. my question how does the original circle object in ArrayList take values from the above function? this is how we called it

public boolean onTouchEvent(MotionEvent event) {
    // 1. get the x and y of MotionEvent
    float x = event.getX();
    float y = event.getY();
    // 2. find circle closest to x and y
    Circle cr = findCircleClosestToTouchEvent(x, y);
    // 3. find cross closest to x and y
    Cross cx = findCrossClosestToTouchEvent(x, y);
    // 4. compute euclid distances to find which is
    // closer - circle or cross
    float dtcr = euclidDist(cr.getCurrentX(), cr.getCurrentY(), x, y);
    float dtcx = euclidDist(cx.getMidX(), cx.getMidY(), x, y);
    // 5. if distance to closest circle is smaller
    // handle the circle; otherwise, handle the cross
    if (dtcr < dtcx) {
        handleTouchedCircle(event, cr);
    } else {
        handleTouchedCross(event, cx);
    }
    return true;
}

I searched a little people say there is no pass by reference in Java (it has different meaning from c++), so what is the criteria thank you

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Pass by reference is also a Java term. What does `findCircleClosestToTouchEvent` do? Does it return from that Arraylist? – OneCricketeer Nov 12 '17 at 19:02
  • Possible duplicate of https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value. – luckydog32 Nov 12 '17 at 19:43
  • @cricket_007 yes. here is the code that calls private Cross findCrossClosestToTouchEvent(float x, float y) { Cross c = mCrosses.get(0); float dist = euclidDist(c.getMidX(), c.getMidY(), x, y); float tempdist = 0; for (Cross cx : mCrosses) { tempdist = euclidDist(cx.getMidX(), cx.getMidY(), x, y); if (tempdist < dist) { c = cx; dist = tempdist; } } return c; } it finds the nearest circle. – user3503164 Nov 13 '17 at 05:03

0 Answers0