# Trying to make a grid

• 07-28-2012, 04:30 PM
Maarten
Trying to make a grid
Hello,
I'm trying to make a grid with 5's in it. But it only shows the lines and only one 5 in the upper left corner. "speelveld" is a 3 by 3 2-dimensional array with 5's in all places.This is my code:

Code:

public void display(Graphics g){

for (int rij=0; rij<speelveld[0].length; rij++)
for (int kolom=0; kolom<speelveld[0].length; kolom++)

g.drawRect(startX+(kolomSprong * kolom),(startY-rijSprong) + (rijSprong * rij),kolomSprong,rijSprong);
g.drawString(Integer.toString(speelveld[rij][kolom]),(startX +(kolomSprong/2)+ (kolomSprong * kolom)),startY-(rijSprong/2) + (rijSprong *rij));

if I put the g.drawString line in first, then it shows all the 5's and only one rectangle in the upper left corner.
Anyone any idea why I dont get both the rectangles and the fives?
• 07-28-2012, 05:11 PM
DarrylBurke
Re: Trying to make a grid
Near impossible to say. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. A single snippet out of context doesn't show what might be wrong.

db
• 07-28-2012, 05:17 PM
JosAH
Re: Trying to make a grid
Check if 'rijSprong' and 'kolomSprong' have sensible values.

kind regards,

Jos
• 07-28-2012, 05:51 PM
Maarten
Re: Trying to make a grid
In reaction to Darryl I made my program as small as possible and I can post it as a whole. The problem/question is the same, if I put g.drawRect first it makes the lines but not the 5's. If I put g.drawString first it puts in the 5's but not the lines. Here is the code:

Code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Boterkaas2 extends Applet {

private Veld boter;

public void init() {
boter = new Veld(3,3);
}

public void paint(Graphics g) {
boter.display(g);
}

class Veld  {

private int[][] speelveld;
int startX = 80;
int startY = 80;
int rijSprong = 30;
int kolomSprong = 30;
public int rij;
public int kolom;

public Veld(int rijen, int kolommen) {
speelveld = new int [rijen][kolommen];
for (int rij=0; rij<rijen;rij++)
for (int kolom=0; kolom<kolommen;kolom++)
speelveld[rij][kolom]=5;

}

public void display(Graphics g){

for (int rij=0; rij<speelveld[0].length; rij++)
for (int kolom=0; kolom<speelveld[0].length; kolom++)

g.drawString(Integer.toString(speelveld[rij][kolom]),
(startX + (kolomSprong * kolom)),startY + (rijSprong *rij));
g.drawRect(startX+(kolomSprong * kolom),
(startY-rijSprong) + (rijSprong * rij),kolomSprong,rijSprong);    }
}
}

• 07-28-2012, 07:48 PM
HelloWorld1234
Re: Trying to make a grid
you have to put the drawString and the drawRect inside the for loops. and then you should just have to fix the positions
• 07-28-2012, 08:00 PM
Maarten
Re: Trying to make a grid
solved, thank you!
• 07-28-2012, 08:18 PM
HelloWorld1234
Re: Trying to make a grid
Your welcome. Remember that i you don't use the "{" and the "}" it will ONLY loop the next line and nothing more than that
• 07-28-2012, 08:51 PM
Maarten
Re: Trying to make a grid
yes its actually something I rememberd after your post, wont forget it again now, cheers