0
public void drawboard(Graphics g){
    g.setColor(Color.yellow);
    for (int row = 0; row < board.length; row++){
        for (int col = 0; col < board[row].length; col++){
                g.drawString("X", col, row);
            }
    }

All I get is a yellow square that is 35x30 (variables for row and col in another part of the program). I can't see any "X" at all. I have tried to space it out in case it was all mashed together with col+10, row+10, but it doesn't affect it. I switched col and row with the same affect.

Thank you.

johnny
  • 19,272
  • 52
  • 157
  • 259
  • 1
    No time to write out an answer, but remember that `drawString`'s last two parameters are *pixel* values, not *character* values. At best, this will draw a bunch of X's almost on top of each other (since you're only drawing them 1 pixel apart and they're wider than 1 pixel). – Michael Myers Feb 05 '10 at 23:30
  • thank you. I did not know that about the pixel part. I was using to using the console or nice web apps :). – johnny Feb 06 '10 at 00:36

2 Answers2

4

The drawString second and third argument are in pixels. You're just printing lots of X's on themselves, offset by 1 pixel and again and again, so of course all you get is a big blob.

What you want is to multiply by the width/height of your rows and cols like this:

g.drawString("X", col * columnWidth, row * rowHeight);
SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
  • Thank you (everyone). Can you tell me why I am multiplying? I'm sure it's obvious but I don't understand. – johnny Feb 06 '10 at 00:36
  • so I need another variable for width and height in pixels? Is that correct? – johnny Feb 06 '10 at 00:47
  • @johnny, Yes you need to determine the column width and row height somehow. You may find `g.getFontMetrics()` useful for this. – finnw Feb 06 '10 at 07:21
-1

There is nothing wrong with this code. For this piece of code, of course you see yellow rectangle, as Xs are spaced only 1 pixel apart.

If you provide drawString with sufficient coordinates, you should see your X at that coordinates. So, if you space col and row variables in your loop like you have explained, you should see several Xs in several rows/columns. (But don't forget to change the board array in that case, as it is possible the loop will not execute at all, if you space them too far apart).

Maybe you can tell us where do you call that code from? Is it executed in paintComponent method or do you call it manually from some other thread?

Grega Kešpret
  • 11,827
  • 6
  • 39
  • 44
  • 1
    It's not because a piece of code work as "expected by the code" that it's not wrong: most programs would be correct with such reasoning and bug inexistent. If he wants to see a board made of 'X's and all he sees is a yellow rectangle then there's something wrong with his code ;) – SyntaxT3rr0r Feb 05 '10 at 23:34
  • I assumed that he knew that, since he wrote "I have tried to space it out in case it was all mashed together with col+10, row+10, but it doesn't affect it. I switched col and row with the same affect.". This tells me, that he knows why there is yellow rectangle, so I assumed that there was something else that was wrong and that he wants us to find out why. That is the reason I wrote "there is nothing wrong with this code" in the first place, so he wouldn't worry too much about this code, but started looking solutions elsewhere. – Grega Kešpret Feb 06 '10 at 09:30