I am trying to create a nested loop which will allow me to display something a particular amount of times, but the problem is that it is only placing one element and not the others. How do I set the coordinates for that?
int rows, cols;
rows = 7;
cols = 6;
for (int i=0; i <rows; i++); {
for (int j=0; j< cols; j++);
}
Maybe because you hardcoded the circle position and it is therefore the same for all circles?
canvas.drawCircle(80, 155, 40, white);
what about calculating the position?
int x = i * radius + 10; // or whatever you wanna calculate here...
int y = j * radius + 10;
canvas.drawCircle(x, y, 40, white);
Make sure you're updating the circle coordinates on each loop cycle! Right now they are hardcoded. This may help:
int rows, cols;
rows = 7;
cols = 6;
for (int i=0; i <rows; i++); {
for (int j=0; j< cols; j++) {
canvas.drawCircle(80 + (80*i), 155 + (155*j), 40, white);
}
}
I just used the same values for x and y as the offsets, although you may want to play around with them(80 and 155) to find the look you're going for.
Your for loop doesen't change the position of the circle since you only say to draw it on 80 and 155
try something like this
int rows, cols;
rows = 7;
cols = 6;
//initial x and y positions
float initialX =80;
float initialY =155;
int radius = 40;
for (int i=0; i <rows; i++); {
//you need to reset it for each circle
float drawX = initialX;
for (int j=0; j< cols; j++){
canvas.drawCircle(initialX, initialY, radius, white);
//where 10 will be the little space you want between circles
drawX+=radius*2+10;
}
initialY+=radius*2+10;
}
Related
I need to flip an image over an imaginary diagonal going from the top right to bottom left of an image. This is what I have so far. getBasicPixel(x,y) returns the rgb of the pixel as an int value.
public static void flipD(Picture p){
int h= p.getHeight();
int w= p.getWidth();
int v=0;
for(int i=0; i< h; i++){
for(int j=0; j<w; j++){
v=p.getBasicPixel(i,j);
p.setBasicPixel(w-1-i, w-1-j, v);
}
}
}
This should just be a matter of trading x and y. So, if one were doing this operation on an array it would look like this.
int portrait[][] = { {1,2}, {2,4}, {3,6} };
int landscape[][] = new int[2][3];
for (int row=0; row < portrait.size; row++) {
for (int col=0; col < portrait[0].size; col++) {
landscape[col][row]=portrait[row][col];
}
}
These objects all have an attribute thats called number, i want to now add to every object in this 2d array a number, starting from 0 to 99.
So tiles[0][0] has the number 0 and tiles[9][9] has number 99. tiles[3][0] should have number 2.
I tried putting this in a nested for loop like this:
for(int x = 0; x < mapWidth; x++)
{
for(int y = 0; y < mapHeight; y++)
{
for(int i = 0; i <100; i++){
tiles[x][y].nummer(i);
}
}
}
but with this the nummer is always 99? it isnt 0 to 99
How can i do this in a for loop or something?
SO MY QUESTION IS:
how can i make it so that the nummer for the tiles goes from 0 to 99?
int counter = 0;
for(int y = 0; y < mapHeight; y++)
{
for(int x = 0; x < mapWidth; x++)
{
tiles[x][y].number(counter);
counter++;
}
}
it's because you are running a loop that sets the number from 0 to 99 on every tile, so you end up with 99 everywhere.
Instead you should do something like:
for(int i = 0 ; i < 100 ; i++) {
tiles[i%10][i/10] = i;
}
You could also just use a variable to keep the counter
int num = 0;
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
tiles[x][y].nummer(num);
num++;
}
}
I need some help with creating a grid in java AWT. You get a certain number of columns, for now let's say 13.
for(int i = 0; i < 13; i++)
{
Graphics.fillRect((i * 15), 10, 10, 10);
}
Graphics takes in x, y, width, height.
This results in;
XXXXXXXXXXXXX
This will put all cubes next to each other. This is not what I want. I need the y position to go down by 15 pixels every time 4 cubes have been drawn.
The result would be
XXXX
XXXX
XXXX
X
Thanks for your time!
Basically, you have to increment the x, and every time you reach the limit, you bring the x back to zero, and increase the y:
// These should be constants, defined at the class level
public static final int NUM_RECTANGLES = 13;
public static final int NUM_COLUMNS = 4;
// And your loop
int col = 0;
int row = 0;
for(int i = 0; i < NUM_RECTANGLES; i++)
{
Graphics.fillRect((col * 15), 10 + (row * 15), 10, 10);
col++;
if( col == NUM_COLUMNS ) {
col = 0;
row++;
}
}
As you see, after each rectangle, you increase the column. If you get to the limit for the column (remember you started from 0), you move to the next line. This means the column is again 0, and the row is increased.
Of course, if your 13 and 4 are actually parameters, ignore my remark about constants.
Just use a double for() loop
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++) {
Graphics.fillRect(i * 15, j * 15, 10, 10);
}
}
This will create a 4x4 grid of rectangles. If you want to limit it to a 4x4-like grid that only has 1 rectangle in the last row, which it seems like you want, use j < ((i == 3) ? 1 : 4) as your termination condition in the second for loop, or use a simple if condition like so:
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++) {
Graphics.fillRect(i * 15, j * 15, 10, 10);
if(i == 3) break;
}
}
edit: Generic Solution
int nRow, nCol, nLastCol;
//Initialize values
for(int i = 0; i < cRow; i++){
for(int j = 0; j < ((i == nRow - 1) ? nLastCol : nCol); j++) {
Graphics.fillRect(i * 15, j * 15, 10, 10);
}
}
You need to set how many columns and keep track of which column you're drawing so that you can increment the row. On a side note, I also wouldn't put a magic number in the loop, instead make that a variable (just good programming practice). Something like this:
int numRectangles = 13;
int numColumns = 4;//set this to however many columns you need
int currentRow = 1;//start at one, not zero
int tempRow = 1;
for(int i = 0; i < numRectangles; i++)
{
Graphics.fillRect((i * 15), (tempRow * 10), 10, 10);
currentRow++;
if(currentRow > numColumns) {
currentRow = 1;
tempRow++;
}
}
I need to try and duplicate and image, created by array(a), both vertically and horizontally to make a square full of the repeating image. So starting with 1 square image and I want to duplicate it 2 times horizontally and 2 times vertically it will create another image with 4 of the initial image, 2x2.
public static int[][] replicate(int[][] a) {
int[][] replicated = new int[a.length * 2][a[0].length * 2];
for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i][j + r * a[i].length] = a[i][j];
}
}
}
for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i + r * a.length][j] = a[i][j];
}
}
}
return replicated;
}
This gets them across the horizontal for 1 line, and vertical for 1 line but does not fully fill in. So if I ran this I would be missing the bottom right of the 4 images. I'm having trouble putting them together.
public static int[][] replicate(int[][] image) {
int[][] replicated = new int[a.length * 2][];
for (int y=0; y<image.length; y++) {
int[] column = image[y];
replicated[y] = new int[column.length * 2];
replicated[y + image.length] = new int[column.length * 2];
System.arraycopy(column, 0, replicated[y], 0, column.length);
System.arraycopy(column, 0, replicated[y], column.length, column.length);
System.arraycopy(column, 0, replicated[y + image.length], 0, column.length);
System.arraycopy(column, 0, replicated[y + image.length], column.length, column.length);
}
return replicated;
}
There might be some problems with this code since I wrote it not in front of a compiler, but I think it gets the point across.
Also, if this is for painting the image, you should consider using a TexturePaint instead.
I am coding a mod for minecraft, and was confronted by a confusing math problem.
I want to find the ID of all blocks around a center block.
To do this I wanted to loop through a 3x3 square of blocks and return which ones are the blocks I want.
The parameters I have to work with are the X, Y and Z coords.
I'm guessing the best bet would be to use 3 for loops, one for each axis, right?
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
for(int k = 0; k < 3; k++){
//do something
}
}
}