I have created a for loop to produce a grid map. When I click each grid on the map I get X and Y of the grid. When map width is greater than map length, everything is fine, but when attempt to create a map where length is greater than with, the returned x becomes the y, and y becomes the x. The issue is at the second for loop when creating the map but I cannot figure it out.
if(mapWidth>mapLength) {
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
for(int j=0; j<i+1; j++) {
x = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
} else if(mapWidth<mapLength) { //problematic map is created after this condition
for (int i = 0; i < mapWidth * mapLength; i++) {
x = i / mapLength;
for(int j=0; j < i+1; j++){
y = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Maps look like this:
Well, maybe I didn't understand well what you expect but I don't think you have to make a special case for the case where mapWidth< mapLength.
Furthermore, I don't get what you intend to do with your nested loop except using CPU resources?
for(int j=0; j<i+1; j++){
x = j % mapLength;
}
When it is left, you will always have x = i % mapLength
Furthermore, as flkes suggested, why don't you use nested loops?
for (int y=0; y < mapLength; y++) {
for(int x=0; x < mapWidth; x++){
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Could you try this code:
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
x = i % mapLength;
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
Related
I just wrote the code below and thought is there any way to do the same without using the nesting loops and how clean and efficient will it be?
for (int y = Y_LEVEL_START; y <= Y_LEVEL_STOP; y++) {
for (int z = 0; z < CHUNK_SIDE; z++) {
for (int x = 0; x < CHUNK_SIDE; x++) {
if (chunk.getBlock(x, y ,z).getType() == ANCIENT_DEBRIS)
report.debrisFoundAt(x, y, z);
}
}
}
double numbers[][];
numbers = new double[22][9];
for(int x = 0; x<22; x++) {
for(int y = 0; y <9; y++)
{
numbers[x][y] = (int)(Math.random()*192)+1;
System.out.print(numbers[x][y]+ "");
System.out.println();
}
Trying to display the array within a table/index but when I do, it just display the random numbers vertically. Idk how to fix it. Sorry for the nooby code.. :(
In java two-dimensional array in java is just an array of array, and a small mistake while iterating it. Add System.out.println(); in outer for loop
for(int x = 0; x< 22; x++) { // for every array in outer array
for(int y = 0; y < 9; y++) { //for every double in each inner array
numbers[x][y] = (int)(Math.random()*192)+1;
System.out.print(numbers[x][y]+ " ");
}
System.out.println();
}
If you separate construction and display, it may be clearer:
double numbers[][] = new double[22][9];
// construction
for(int x = 0; x<22; x++)
for(int y = 0; y <9; y++)
numbers[x][y] = (int)(Math.random()*192)+1;
// display
for(int x = 0; x<22; x++){
for(int y = 0; y <9; y++)
System.out.print(numbers[x][y]+ "\t");
System.out.println("");
}
I have a 2d array called tiles[x][y] which goes till 9 so has 100 things inside of it.
How can I get another array and put everything from the 2d array into the normal array?
int counter = 0;
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tiles[y][x] = new loopVak(Color.WHITE, x*tileWidth, y*tileHeight);
}
}
This is how the 2d array is made, mapwidth and mapheight is 10.
If you want to convert tiles to a new 1D Array then you can simply do something like this:
int k = 0, newArray[] = new loopVak[100];
for(int i = 0; i < mapWidth; i++) {
for(int j = 0; j < mapHeight; j++) {
newArray[k++] = tiles[i][j];
}
}
If you do not want the 2D array in the first place then you can do something like this:
int counter = 0, newArray[] = new loopVak[100];
for(int x = 0; x < mapWidth; x++) {
for(int y = 0; y < mapHeight; y++) {
newArray[counter++] = new loopVak(Color.WHITE, x * tileWidth, y * tileHeight);
}
}
I am implementing my own Image Convolution method in Java, it is supposed to be general so I can run any kernals through it. It works and I am able to output things, but the output is wrong. The right side of the image appears on the left hand side, and the image seems to triplicate throughout it with varying intensities. This occurs regardless of the kernal I am running (I have tried 9x9 and standard 3x3) and very similar things occur. I have played with this and changed everything to get it to work but it is still behaving incorrectly.
for (int i = 0; i < (image.getWidth()-mask.length); i++)
{
for (int j = 0; j < (image.getHeight()-mask.length); j++)
{
int sum = 0;
for (int w = 0; w < mask.length; w++)
{
for (int z = 0; z < mask.length; z++)
{
sum += arr[w + i][z + j] * mask[w][z];
}
}
if(sum < 170)
{
convArray[i][j] = 0;
}
else
{
convArray[i][j] = 255;
}
I have an array that I'm trying to blur using a kernel, but the loop doesn't finish for some reason, here's the code:
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 128; y++) {
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
nlm2[x][y] += 100 * (int) ((float) nlm[x][y]*(float)kernel[(kx+3)*(ky+3)-1]);
System.out.println(x+" "+y);
System.out.println(kx+" NLM: "+(float) nlm[x][y]);
System.out.println(ky+" Kernel: "+(float)kernel[(kx+3)*(ky+3)-1]);
}
}
}
}
It seems to stop after
x = 0; y = 6
kx = ky = -2
There are no errors in the console, and it shows another print screen after this little loop-de-loop.
This lines might be the problem.-
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
You're incrementing x and y respectively, instead of kx and ky.
I'm guessing you really meant.-
for (int kx = -2; kx <= 6; kx ++) {
for (int ky = -2; ky <= 6; ky ++) {