So im trying to make an adroid application that draws a stream of circles as touchevent takes place. If i draw on the left side of the screen it should draw a green circle, and if its on the right , it should draw a blue circle. The app is doing this, but it changes the color of all the circles already drawn. SO i made a Draw circle Class and a array list of objects to treat each cricle as an individual objec, still not working Even after a Touchup event. the code for it is given below
private class Drawcirlce {
public Drawcirlce(Canvas c) {
// TODO Auto-generated constructor stub
for (Point point : points) {
if(flag==true)
c.drawCircle(point.x, point.y, 5, paint);
else
c.drawCircle(point.x, point.y, 5, p2);
}
invalidate();
}
/*for (Point point : points) {
if(flag==true)
mcan.drawCircle(point.x, point.y, 5, paint);
else
mcan.drawCircle(point.x, point.y, 5, p2);
}*/
}
public void onDraw(Canvas canvas) {
i++;
//Drawcirlce d=new Drawcirlce();
dc.add(new Drawcirlce(canvas));
Log.d(TAG, "i: " + i);invalidate();
}
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();
if(point.x>200){
paint.setColor(Color.BLUE);
flag=true;
}
else{
p2.setColor(Color.GREEN);
flag=false;
}
points.add(point);
//dc.add(new Drawcirlce(mcan));
invalidate();
Log.d(TAG, "point: " + point);
return true;
}
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();
if(point.x>200){
paint.setColor(Color.BLUE);
flag=true;
}
else{
p2.setColor(Color.GREEN);
flag=false;
}
points.add(point);
//dc.add(new Drawcirlce(mcan));
invalidate();
Log.d(TAG, "point: " + point);
return true;
}
Any one know aht im doing wrong, or if theres a way around this?
Your
Drawcirlceclass needs to have aPaintobject in it. (You can make this public to the class, or give itgetPaint()andsetPaint(), whichever.) Then, when you callc.drawCircle(point.x, point.y, 5, paint);, call it with thePaintobject in the class instead.Something like this:
Then, in your
onTouchevent, you do something like this:Lastly, you would have to change your
onDrawevent.This code, in effect, assigns a circle the ability to have its own
Paintobject. Then, when touching, you create aPaintto give it. Lastly, when drawing, you use thatPaintinstead of the current one.