Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Randomly Generated Map Graphics- Troubleshoot Help Needed

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Location
    MN, USA
    Posts
    4
    My Mood
    Cynical
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Randomly Generated Map Graphics- Troubleshoot Help Needed

    The java program I have written compiles and the applet runs, but the program does not run the way it should. There should be many small tiles of random color but the program currently only fills the single top-left tile. I am thinking that there is something wrong with my loops. Any help or insight would be greatly appreciated.

    The code:
    import java.awt.*;
    import java.applet.*;
    import java.util.Random;
     
    public class WorldExplorerG extends Applet
    {
    	public void paint (Graphics g)
    	{
    		t= new int [100][100];
    		Random generator = new Random();
    		for (int i=0; i<100; i++)
    		{
    			for(int j=0; j<100; j++)
    			{	
    				t[i][j]=generator.nextInt(8);
    			}
    		}
     
    		Color iceBlue= new Color(223,255,255);
    		Color grassGreen= new Color(0,120,0);
    		Color gravelTan= new Color (200,180,130);
    		Color sandYellow= new Color(255,255,203);
    		Color stoneGray= new Color(92,92,92);
    		Color snowWhite= new Color (245,245,245);
    		Color seaBlue= new Color(0,51,164);
     
    		for (int s=0; s<100; s++)
    		{
    			int l=0;
    			for(int w=0; w<100; w++)
    			{	
    				int k=0;
    				g.fillRect(5*l,5*k,5,5);
     
    				if(t[s][w]==1)
    				{
    					g.setColor(grassGreen);
    				}
     
    				if(t[s][w]==2)
    				{	
    					g.setColor(gravelTan);
    				}
     
    				if(t[s][w]==3)
    				{
    					g.setColor(sandYellow);
    				}
     
    				if(t[s][w]==4)
    				{
    					g.setColor(stoneGray);
    				}
     
    				if(t[s][w]==5)
    				{
    					g.setColor(snowWhite);
    				}
     
    				if(t[s][w]==6)
    				{
    					g.setColor(seaBlue);
    				}
     
    				if(t[s][w]==7)
    				{
    					g.setColor(iceBlue);
    				}
    				k++;
    			}
    			l++;
    		}
    	}
    	public int[][] t;
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Randomly Generated Map Graphics- Troubleshoot Help Needed

    Step through the code either by hand with a pen and paper, or by adding some println statements as a debugging tool (or if your experience allows, use a debugger). What would be (or is if you use a debugger/println) the value of k for each iteration of the inner loop? I'm guessing once you do this, you will see a problem (hint: variable scope)

  3. The Following User Says Thank You to copeg For This Useful Post:

    achamb (November 13th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Location
    MN, USA
    Posts
    4
    My Mood
    Cynical
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Randomly Generated Map Graphics- Troubleshoot Help Needed

    Quote Originally Posted by copeg View Post
    Step through the code either by hand with a pen and paper, or by adding some println statements as a debugging tool (or if your experience allows, use a debugger). What would be (or is if you use a debugger/println) the value of k for each iteration of the inner loop? I'm guessing once you do this, you will see a problem (hint: variable scope)
    Seeing as how the program is supposed to be run as an applet, I do not know how I would use a print or println statement.

    I am assuming that your nuanced reply is suggesting that the value of k is not changing, and I will have to take another look at it.

    Thanks.
    EDIT:
    I found the error and pulled L and k out so that they are not reset each time the loop runs.

Similar Threads

  1. [SOLVED] Troubleshoot Help: Darts Hit or Miss using a (for) loop.
    By achamb in forum Loops & Control Statements
    Replies: 2
    Last Post: October 22nd, 2012, 10:58 PM
  2. Help me to troubleshoot the errors
    By Siddhantnaik in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2012, 03:47 PM
  3. Game of Life Matrix...Needs troubleshoot, specific output
    By Eriosblood in forum Collections and Generics
    Replies: 12
    Last Post: September 11th, 2012, 10:59 PM
  4. Replies: 9
    Last Post: September 1st, 2012, 07:25 AM
  5. thought i had it, then i sneezed and lost it (randomly generated numbers)
    By fakeClassy in forum Java Theory & Questions
    Replies: 13
    Last Post: July 17th, 2011, 04:16 PM

Tags for this Thread