I’m drawing circle and want that when lines match together then generate error.I have done some coding and this code is able to draw lines on touch but problem is that when line match together its unable to generate error.So can anybody suggest me what i’m doing wrong.
Code
public class DrawView extends View implements View.OnTouchListener {
private static final String TAG = "DrawView";
List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
DrawView.this.setOnTouchListener(DrawView.this);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
}
public void onDraw(Canvas canvas) {
for (Point point : points) {
canvas.drawCircle(point.x, point.y, 5, paint);
// canvas.drawLine(point.x, point.y, 5, 0, paint);
// canvas.drawPaint(p);
// Log.d(TAG, "Painting: "+point);
}
}
public boolean onTouch(View view, MotionEvent event) {
// if(event.getAction() != MotionEvent.ACTION_DOWN)
// return super.onTouchEvent(event);
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
for (int i = 0; i < points.size(); i++) {
if (point.equals(points.get(i))) {
System.out
.println("*****************************ERROR*******************************************");
} else {
System.out.println("Value of points" + points);
System.out
.println("***********************ELSE***********************************");
}
}
points.add(point);
invalidate();
Log.d(TAG, "point: " + point);
return true;
}
}
class Point {
float x, y;
public boolean equals(Object o) {
if (!(o instanceof Point))
return false;
Point p = (Point) o;
return x == p.x && y == p.y;
}
public String toString() {
return x + ", " + y;
}
}
You need to implement equals()-method for your Point class. The default implementation probably won’t compare your x & y attributes.