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 7713685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:56:05+00:00 2026-06-01T01:56:05+00:00

I’m making a new game that generates a huge grid (lets say 1000×1000). The

  • 0

I’m making a new game that generates a huge grid (lets say 1000×1000). The player starts in the middle and moves around looking for “points”. The player can only see a portion of the larger grid in the window (15×15). Right now I just have a huge array of rectangles each that paint a bufferedimage of a dirt block that I created. Question: Is there a better variable type I should be using to store all these images?

This is what my Dirt class looks like (holds the rect, and the image which is generated at the beginning):

public class Dirt extends Spot{
    private int index;

    public Dirt(int temp){
        index = temp;
    }

    public Image getImageIndex(){return index;}
}

And here is apart of my Board class that draws all the Dirt:

public class Board extends JPanel{
    private final int BLOCK_SIZE;                                   //Holds the size of each block
    private final int SIZE;                                     //Holds the size of the board
    private DirtImages[] imgs_Dirt = new DirtImages[20];    //Holds 20 random dirt images - generated at begtinning

    private Spot[][] spots;

    public Board(int size, int blocksize){
        SIZE = size;
        BLOCK_SIZE = blocksize;
        //Board
        setSize(SIZE,SIZE);
        //Timer Label
        add(JTimerLabel.getInstance());

        //Create 20 random Images of dirt to use for the rest of dirts
        for(int i = 0; i < 20; i++){
            imgs_Dirt[i] = new DirtImages(new Rectangle(0,0,BLOCK_SIZE,BLOCK_SIZE));
            add(imgs_Dirt[i]);
        }

        //Create Dirt
        spots = new Dirt[500][500];
        java.util.Random randomGenerator = new java.util.Random();
        for(int i = 0; i < spots.length; i++){
            for(int j = 0; j < spots.length; j++)
                spots[i][j] = new Dirt(randomGenerator.nextInt(20));
        }
    }

    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //Draw Grid #First
        for(int i = 0; i < spots.length; i++){
            for(int j = 0; j < spots.length; j++)
                if(spots[i][j] != null)
                    g2d.drawImage(imgs_Dirt[((Dirt)spots[i][j]).getImageIndex()].getImage(), BLOCK_SIZE*i,BLOCK_SIZE*j,BLOCK_SIZE,BLOCK_SIZE, null);
        }

        Toolkit.getDefaultToolkit().sync();
        g2d.dispose();
        requestFocus();
    }

Just to clarify. I create 20 Dirt images so that the dirt (when painted) don’t look like they’re tiled but are random. So in my array of Dirt, each Dirt points to a random image.

Additional Question: Now that I create my huge grid, how would I make it so the player starts at the center and I draw the surrounding cells. Currently I start at the beggining of the array in the top left corner of my array. Should I create a boolean flag for each dirt of whether or not it should be drawn?

  • 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-06-01T01:56:06+00:00Added an answer on June 1, 2026 at 1:56 am

    If you have 1,000,000 Dirts and only 20 images, it’s kind of a waste to store an image in each dirt. You can use an index instead. So to draw, instead of drawing

    ((Dirt)spots[i][j]).getImage()
    

    you would draw

    imgsDirt[((Dirt)spots[i][j]).getImageIndex()]
    

    For the updated question:

    You seem to be drawing your whole grid(0-1000,0-1000). What you really want is just to paint the 15×15 ‘viweable’ area. So instead of

        //Draw Grid #First
        for(int i = 0; i < spots.length; i++){
            for(int j = 0; j < spots.length; j++)
    

    it would be

        //Draw Grid #First
        for(int i = player.xpos-7; i < player.xpos+7; i++){
            for(int j = player.ypos - 7; j < player.ypos+7; j++)
    

    You would need an appropriate shift in your drawImage method to make sure it still starts drawing at the upper left of your window.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT

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.