-
2D Array help!
Hi all, i was hoping one of you guys could help me out.
I have a class which contains an 2D array called raster[10][16]
Each "postition in the array contains either a 1 or a -1 now i want to make a for loop in my paintComponent so that when the loop reads a 1 it creates a certain drawing and when it reads a -1 it creates and other cind of drawing. and the total drawing should be 10 rows containing 16 pieces of 50px each.
this is what i have come up with, but it isnt really working:
Code:
public void teken( Graphics g )
{
int waarde = 0;
for( int rij = 0; rij < raster.length; rij++ )
for( int kolom = 0; kolom < raster[0].length; kolom++ )
{
waarde = raster[rij][kolom];
for( int i =0; i < 15; i++ )
{
x += 50;
if( waarde == -1 )
{
g.setColor( Color.blue );
g.drawRect(x, y, grootte, grootte);
g.setColor( Color.black );
g.fillRect(x, y, grootte, grootte);
}
if( waarde == 0 )
{
g.setColor( Color.yellow );
g.drawRect(x, y, grootte, grootte);
g.setColor( Color.black );
g.fillRect(x, y, grootte, grootte);
}
if( waarde == 1 )
{
g.setColor( Color.black );
g.fillRect(x, y, grootte, grootte);
g.setColor( Color.yellow );
g.drawRect(x, y, grootte, grootte);
g.setColor( Color.white );
g.fillOval(x, y, grootte/2, grootte/2 );
}
}
}
}
-
-
when i run the program the only thing i get is one square (i think the last one) but whole the panel should be filled with the squares ( 1 or -1 depens on the position in the array)
-
I don't see that you're ever incrementing y or initializing x at the beginning of the method, and just by using back of the envelope calculations, it looks as if your x will march off way to the right.
You may want to sit with pencil and paper and work out your math a bit more here.
-
but is it right that the for loop looks at every value in every cell of the array and than give the value to "waarde"? and than picks the right one out of the if's?
-
Sure that's fine, but your x will increase by 50 x 15 for each cell in the array and will never 0. So if you have a 10 x 10 array, you'll be trying to draw at x position 100 x 50 x 15 or X position 75000 which doesn't make sense. Again, double, triple check your math.
-
but it doesnt make sense because all it does is paint one square
-
I think that if you want further help on this, you'll want to create a small compilable program that we can run and that demonstrates your problem. One description of this construct can be found here: Short, Self Contained, Correct (Compilable), Example
Much luck!