Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2009, 12:29 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
nazus is on a distinguished road
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:

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 );
					}
				}
			}
	}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-27-2009, 12:54 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
what is "not working"?
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-27-2009, 12:55 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
nazus is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-27-2009, 01:04 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-27-2009, 01:06 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
nazus is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-27-2009, 01:10 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-27-2009, 01:16 AM
Member
 
Join Date: Nov 2009
Posts: 4
Rep Power: 0
nazus is on a distinguished road
Default
but it doesnt make sense because all it does is paint one square
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-27-2009, 01:39 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
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!
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Char Array to String Array Mayur New To Java 8 10-12-2009 12:41 PM
Array length and printing out uninitialized array. nicolek808 New To Java 4 09-10-2009 10:12 AM
How to transfer 1D array in JAVA to 3D array in C fishwater00 New To Java 0 07-31-2009 07:24 PM
How to add an integer to a array element and the store that backinto an array. Hannguoi New To Java 1 03-31-2009 07:40 AM
String array to byte array?! Joe2003 Advanced Java 5 02-28-2009 07:09 AM


All times are GMT +2. The time now is 11:12 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org