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

  • Home
  • SEARCH
  • 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 7496157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:30:34+00:00 2026-05-29T18:30:34+00:00

Hey guys I am making a 2D game and I have generating a random

  • 0

Hey guys I am making a 2D game and I have generating a random grassy background from 4 preset images. My problem is this, my game draws each image from an array which is created on game start using this code:

    public static void createBackground() {
    for(int x = 0; x < 640; x+= 32) {
        for(int y = 0; y < 496; y+= 32) {
            random = new Random();
            int grassTex = random.nextInt(grassTextures.size());

            grassPos.add(grassTextures.get(grassTex));
        }
    }
}

That works fine, but this is where the problem occurs. I am re-drawing every image that was put into that array using this:

    public static void paintBackground(Graphics g) {
    counter = 0;
    for(int x = 0; x < 640; x+= 32) {
        for(int y = 0; y < 496; y+= 32) {
            random = new Random();

            int grassTex = random.nextInt(grassTextures.size());
            g.drawImage(grassPos.get(counter), x, y, null);
            counter++;

        }
    }

}

This causes a drop in fps (It’s not a lot, but it is noticeable). Is there anyway that I can draw all those grass images into one BufferedImage so that it is only one image that is being drawn? Or is there a more efficient way of doing this?

Thanks.

  • 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-29T18:30:40+00:00Added an answer on May 29, 2026 at 6:30 pm

    There is no way to take “the image from Graphics”, as you ask, but it is not very hard to just create BufferedImage and then draw the image into it, instead. If anything, AWT’s imaging model is quite complex, and you’ll have to construct a couple of auxiliary data structures and stuff, but here’s a template which creates 32-bit RGBA images:

    private static final ComponentColorModel colormodel =
        new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                new int[] {8, 8, 8, 8}, true, false,
                                ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    public static BufferedImage makeimage(int w, int h) {
        WritableRaster buf = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h, 4, null);
        return(new BufferedImage(colormodel, buf, false, null));
    }
    

    Once you have a BufferedImage as created by makeimage(), just call getGraphics() on it and paint to your heart’s content.

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

Sidebar

Related Questions

Hey guys I have a problem with an app I'm making. The thing is
Hey guys! I have this little problem: I have one ViewController which adds 2
Hey guys, I have a problem (again). This time I am trying to use
hey guys, I have no luck making a simple background image animation working without
Hey Guys, here is my code for this, the only help i get from
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;
Hey guys i'm making my first game in c++ and I was wondering how
Hey guys. This is a follow-on from this question : After getting the right
Hey guys..this is kind of a weird question but I'm making a webapp in
Hey guys, This is my first time making an app with any kind of

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.