Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7186007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:32:26+00:00 2026-05-28T18:32:26+00:00

Sorry, this is a bit of code, but there’s not much to cut out

  • 0

Sorry, this is a bit of code, but there’s not much to cut out here. This is supposed to read an image(a sprite-sheet of the alphabet) and cut it into smaller subImages that are each individual letter. When a key is pressed, the corresponding letter goes on the screen, but this part is just for creating the actual sub-image.

https://i.stack.imgur.com/O7vv8.png (the image)

package typeandscreen;
(where the imports should be, i just cut them out to save space)
public class Font{

    final int width = 78; //gives the dimensions of the image
    final int height = 32;
    final int rows = 4;
    final int cols = 13;

BufferedImage letters[] = new BufferedImage[rows*cols]; //makes the array of
                                                        //subimages. rows*cols is
                                                        //the number of subimages
void test(){
    try{
        final BufferedImage totalImage = ImageIO.read(new File("ABCabcs.png"));
                //loads the big image itself

This following part is what confuses me. What is i and j for, and why is it adding and multiplying them? This part is for finding out how big the subimages have to be, right? Shouldn’t it just be 4 by 13, which is rows*cols?

    for(int i = 0; i < rows; i++){

        for(int j = 0; j < cols; j++){
            letters[(i * cols) + j] = totalImage.getSubimage(
                j * width,
                j * height,
                width,
                height
            );
        }
    }
    } catch(IOException e){
        e.printStackTrace();
    }
  }
}

I don’t get what the i and j are doing. What am I missing here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:32:27+00:00Added an answer on May 28, 2026 at 6:32 pm

    It should be

     j * width,
     i * height,
    

    And also width and height seem to be too big for the size of a single letter but they are used as such.

    i goes through the rows of letters, j goes through the columns. To get coordinates of letter at position (j,i) you need to multiple j (index of column) by width (which is width of each letter) and i (index of row) by height (of a letter).

    letters is the array of images which correspond to letters.

     letters[(i * cols) + j]
    

    is the standard idiom for putting a rectangular matrix into 1-D array. See the picture:

      0 1 2
    0 A B C
    1 D E F
    

    get stored in an array as

    0 1 2 3 4 5
    A B C D E F
    

    so the index of F in this array will be 1 * 3 + 2 = 5 where i = 1, j = 2 and cols = 3 (because there are 3 columns)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is a bit random, but is it good practice to give
Sorry if this question is a bit open ended, but I'm pretty new to
Sorry if this question seems a bit complex but I think its all related
I know this has a stupid solution but, sorry, I'm little bit confused. I
Sorry if this is a bit of a starter question but I am pretty
Sorry if this is a bit long winded but I thought better to post
Sorry if this is a simple question, but I just can't figure out what
Hello this code was not done by me originally and there are some thigns
I'm sorry if this is a bit of a C-noob question: I know I
I'm sorry if this question is a bit basic, or if I've missed an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.