Results 1 to 8 of 8

Thread: 2D Array help!

  1. #1
    nazus is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default 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:

    Java 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 );
    					}
    				}
    			}
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    nazus is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    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)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    nazus is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    nazus is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    but it doesnt make sense because all it does is paint one square

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 12:41 PM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 10:12 AM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 07:24 PM
  4. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  5. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •