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:
Code Java:
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;
}
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)
Re: Randomly Generated Map Graphics- Troubleshoot Help Needed
Quote:
Originally Posted by
copeg
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.