0

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);
}   
}
Shibli
  • 5,879
  • 13
  • 62
  • 126
  • I think you're misunderstanding java pass-by-value. See this:http://stackoverflow.com/questions/40480/is-java-pass-by-reference – TwilightSun Feb 09 '14 at 06:24
  • The paint in grid might be null, maybe you're getting an NullPointerException. **AND** you should always tell us what the problem is or what the error message is, rather than just saying **doesn't work** – TwilightSun Feb 09 '14 at 06:26
  • please post your logcat –  Feb 09 '14 at 06:45

0 Answers0