There are two classes: Renderer which extends View and Grid. In the onDraw method of Renderer, I want to pass Canvas to the function of Grid. Since Java passes arguments only by value the code below does not work. I mean it does not draw a rectangle on the screen and says that program has stopped on my phone. Any idea?
public class Renderer extends View {
Paint paint;
Grid grid;
public Renderer(Context context, Grid grid) {
super(context);
paint = new Paint();
this.grid = grid;
}
protected void onDraw (Canvas canvas) {
canvas.drawRGB(255, 255, 255);
grid.tempName(canvas); // removing this solves problem
invalidate();
}
}
public class Grid {
Paint paint;
public void tempName (Canvas canvas) {
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
canvas.drawRect(100, 100, 200, 200, paint);
}
}