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