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.

2
  • 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). Commented Feb 5, 2010 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 :). Commented Feb 6, 2010 at 0:36

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you (everyone). Can you tell me why I am multiplying? I'm sure it's obvious but I don't understand.
so I need another variable for width and height in pixels? Is that correct?
@johnny, Yes you need to determine the column width and row height somehow. You may find g.getFontMetrics() useful for this.
-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?

2 Comments

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 ;)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.