0

I have been trying to draw a board using java but every time it runs I get an error and the code stops running. It's the very last line of code causing the problem but i am unable to see why.

protected void onDraw(Canvas canvas) {
    //Draw the background...
    Paint background = new Paint();
    background.setColor(getResources().getColor(R.color.game_background));
    canvas.drawRect(0, 0, getWidth(), getHeight(), background);

    //Draw the board
    //Draw the selection


//Draw the board....
//Define colours for grid lines
Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.game_hilite));
Paint light = new Paint();
light.setColor(getResources().getColor(R.color.game_light));
Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.game_dark));
Paint tile1 = new Paint();
dark.setColor(getResources().getColor(R.color.Tile_1));



//Draw the minor grid lines

for (int i = 0; i <11; i++) {
    canvas.drawLine(0, i * height, getWidth(), i * height, dark);
    canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, dark);
    canvas.drawLine(i * width, 0, i * width, getHeight(), light);
    canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), dark);    
}
//Draw the major grid lines
for (int i = 0; i < 11; i++) {
    if (i % 3 != 0)
        continue;
    canvas.drawLine(0, i * height, getWidth(), i * height, dark);
    canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, dark);
    canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
    canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), dark);
}

//Draw the numbers .... 
//Define color and style for numbers 
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
foreground.setColor(getResources().getColor(R.color.game_foreground));
foreground.setStyle(Style.FILL);
foreground.setTextSize(height * 0.75f);
foreground.setTextScaleX(width / height);
foreground.setTextAlign(Paint.Align.CENTER);

//Draw the number in the center of the tile
FontMetrics fm = foreground.getFontMetrics();
//Centering in X: use alignment (and X at midpoint)
float x = width / 2;
//Centering in Y: measure ascent/decent first
float y = height / 2 - (fm.ascent + fm.descent) / 2; 
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            canvas.drawText(this.game.getTileString(i,j), i * width + x, j * height + y, foreground);
        }
    }

}
Jarrod
  • 9,349
  • 5
  • 58
  • 73
  • You have to give us more details than "I get an error". There are a thousand things that can go wrong with this code. What is the exact error you're getting? – GJK Apr 27 '13 at 14:20
  • Well its an android app that's being made it runs and then when clicking the start to produce the board and numbers all it says is "unfortunately, File_Name has stopped" – Julie Flatman Apr 27 '13 at 14:25
  • I don't do Android programming, but I'm 100% sure there's a way to do some sort of logging. You need to catch the error that is being thrown and log the stack trace. Without that, it's going to be pretty hard to help. See this for logging instructions: http://stackoverflow.com/questions/2116260/logging-to-a-file-on-android – GJK Apr 27 '13 at 14:28
  • ok this was the error in the log 04-27 14:23:46.615: E/AndroidRuntime(2078): at dalvik.system.NativeStart.main(Native Method) – Julie Flatman Apr 27 '13 at 14:32
  • That just tells us that the error was thrown from somewhere in your main function (which is ALL of your code). You need to find the line with your file name and the onDraw() function. – GJK Apr 27 '13 at 14:34
  • Sorry i meant this is the errors: 04-27 14:34:25.186: E/StrictMode(813): at java.lang.Thread.run(Thread.java:856) – Julie Flatman Apr 27 '13 at 14:36
  • I think you're going to want to read this: http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – GJK Apr 27 '13 at 14:40
  • see the rest of the onDraw is fine its only that last line: canvas.drawText(this.game.getTileString(i,j), i * width + x, j * height + y, foreground); that causes the problems as i can comment out this line and all works fine but when in i get the stop error. – Julie Flatman Apr 27 '13 at 14:40
  • Look, it's most likely a NullPointerException. I suggest you debug it to find out, as nobody is going to be able to help you with the information you provided. If you tell me what IDE you're using, I can point you to a guide on debugging. – GJK Apr 27 '13 at 14:51
  • I am using Eclipse SDK plugin – Julie Flatman Apr 27 '13 at 14:53
  • This will help you debug your application. Once you have a breakpoint set and are debugging, you should be able to see your problem pretty easily. www.youtube.com/watch?v=JqHYbm9e05A – GJK Apr 27 '13 at 14:55
  • http://java-articles.info/articles/?p=196 – Gilbert Le Blanc Apr 27 '13 at 17:05

0 Answers0