Row 1[ item1, item2, item 3, item 4, item 5, item 6, item 7, item 8]
Row 2[ item1, item2, item 3, item 4, item 5, item 6, item 7, item 8]
Row 3[ item1, item2, item 3, item 4, item 5, item 6, item 7, item 8]
Row 4[ item1, item2, item 3, item 4, item 5, item 6, item 7, item 8]

Say I get a number, say 23. It would be in row 3, item 7.

How would I programatically get the row of this number.

Loop through the row collection. With each row, look for the value you're trying to find. If you find it, use your iterator (maybe a for loop variable) to identify the row.

If you're having trouble with a specific chunk of code, post it and we'll try to find out where you're going wrong.

Thing is, there is no such thing as a row collection. Each row collection is simply just a new row in an image spriteheet. Each row has 8 items in it.

So if I say position 23, I need a way to make it know what row it is in.

use can store your items like this where integer stores the row/position number :

Row1 [HashMap<Integer,Image>,HashMap<Integer,Image>,HashMap<Integer,Image>]
Row2 [HashMap<Integer,Image>,HashMap<Integer,Image>,HashMap<Integer,Image>]
Row3 [HashMap<Integer,Image>,HashMap<Integer,Image>,HashMap<Integer,Image>]

I don't mean that, I should've explained better:

http://i.imgur.com/8YxgeYG.png

Okay, so that's my spritesheet (disregard the formatting of it, please).

Yeah, so say if I say I wanted the 31st. Image (the one I added a smiley face to)

How would I get the row of that image?

declare images as a 2D array

    Image[][] images;

loop through the 2D array to get the 31st image

int n = 31;    //n = id of the image to search
int r = -1;    //r = row of the image
for(int row=0;row<images.length;row++) {
    for(int col=0;col<images[row].length;col++) {
        if(row*col+col==n) {
            r=row; 
            break;
        }
    }
    if(r!=-1)
        break;
}

//r contains 0 based row index

I can't declare an image in an 2D array because:

There is a value (say 22).
There is a .PNG spritesheet.

There are 8 items in a row.

Say if I wanted the 22nd item, I basically want to have the 22nd item returned.

So it would check the first row.
Then it would check the second row.
Check the third row.

Since three rows have been checked 3 * 8 (the amount of rows) = 24, thus the row it is in is the 3rd row.

If there are always 8 items/row then simply divide the item number by 8 to get the row number. Then use modulo 8 to get the position in the row, eg
item number = 22
row = 22/8 = 2 (integer divisiom always rounds down)
item on row = 22%8 = 6 (remainder from division nby 8)

ps: those calulations give 0-based results - ie first row is row 0, first item on row is item 0.

I don't fathom how I could use a modulus operator and a division operator to get the row of which a position is on. 22/8 = 2. 22%8 = 6.How could I use these values to get a row?

How could I use these values to get a row?

depends of array type where those Objects are stored, in most to todays arrays == get a number,from row 3, item 7. (accepts == e first row is row 0, first item on row is item 0. as noticed by @JamesCherrill)

Edited 1 Year Ago by mKorbel

Indeed. But I'm not trying to get the row of an array. I'm trying to get the row of the item specified in a spritesheet image where each row has 8 items.

item number = 22
row = 22/8 = 2 (integer divisiom always rounds down)
item on row = 22%8 = 6 (remainder from division n by 8)

as mentioned by James, if you want the 22nd image in the spritesheet(which contains 8 images per row)

row = 22/8 = 2
column = 22%8 = 6

if you're trying to get 1-based index of the row then just add 1

row = 22/8 + 1 = 3

then if each of your image has width=64pixels and height=64pixels, you can get (top,left) coordinates of the 22nd image by

(column * width,row * height)

I'm supposing there's no padding in between the images

Edited 1 Year Ago by cool_zephyr

Hi Slavi
The solutions you link to are all a bit clunky - either a double-loop search or mis-using the button's name attribute. I would recommend setting the row & col as client properties with putClientProperty and getClientProperty (methods implemented in JComponent, so universally available).
In general, client properties should be your first port of call when you need to associate some extra info with a JComponent

Edited 1 Year Ago by JamesCherrill

Hey James,
I didn't know about that! Sounds pretty good, will have a look at it myself!

This article has been dead for over six months. Start a new discussion instead.