I am trying to draw a rectangle where I am initially providing it values from Activity class and then trying to change its coordinates from a different class.. As far as I am concerned the logic is right, but the invalidate is not working.. I did substantial research but still cannot make it work..
RectangleActivity.java
public class RectangleActivity extends Activity {
private Point dStart = null;
private Point dEnd = null;
final private Handler handler = new Handler();
Check check = new Check(this);
Draw draw ;
Runnable mUpdate = new Runnable()
{
public void run()
{
Toast.makeText(getApplicationContext(), "update Called", Toast.LENGTH_LONG).show();
check.update();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dStart = new Point(0,0);
dEnd = new Point(60,60);
draw = new Draw(this,dStart,dEnd);
setContentView(draw);
handler.postDelayed(mUpdate, 4000);
}
}
Check.java
public class Check
{
private Point start = new Point(80,80);
private Point end = new Point(150,150);
private Context context;
public Check(Context context)
{
this.context =context;
}
void update()
{
Log.d("Aditi", "Update Called");
Draw draw = new Draw(context);
draw.reDraw(start,end);
}
}
Draw.java
public class Draw extends View
{
private Paint paint = null;
private Point start = null;
private Point end = null;
public Draw(Context context)
{
super(context);
paint();
}
public Draw(Context context, Point start, Point end)
{
super(context);
this.start= start;
this.end = end;
paint();
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawRect(start.x,start.y,end.x,end.y,paint);
Log.d("Aditi", "OnDraw Called");
System.out.println("StartX in ondraw is: "+start.x);
System.out.println("StartY in ondraw is: "+start.y);
System.out.println("EndX in ondraw is: "+end.x);
System.out.println("EndY in ondraw is: "+end.y);
}
public void reDraw(Point Start, Point End)
{
Log.d("Aditi", "Redraw Called");
this.start = Start;
this.end = End;
invalidate();
Log.d("Aditi", "Invalidate Called");
System.out.println("StartX in redraw is: "+start.x);
System.out.println("StartY in redraw is: "+start.y);
System.out.println("EndX in redraw is: "+end.x);
System.out.println("EndY in redraw is: "+end.y);
}
public void paint()
{
paint= new Paint();
paint.setColor(Color.YELLOW);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
}
}
please feel free to point me out what am I doing wrong.. Thanks in advance..
In above problem, in onCreate() you took a view using draw = new Draw(this,dStart,dEnd);
and then used in setContentView(draw), but when update() called you again created an object of Draw by using Draw draw = new Draw(this,dStart,dEnd); and call draw.reDraw(start, end) on second draw object, so the instance used in setContentView() is not invalidated, And second draw view object get invalidated but it did not passed in setContentView(), so it is not visible on ui: your solution is just