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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:26:40+00:00 2026-06-17T18:26:40+00:00

I am making a grid-based game that resizes its grid as the window size

  • 0

I am making a grid-based game that resizes its grid as the window size changes. I also may apply color filters for lighting effects in the future. I am concerned about the performance of this code, which draws to the screen an image in one of the grid squares.

public void drawSquares(Graphics g){
        ListIterator<Viewport> iterator = vp.listIterator();
        while(iterator.hasNext()){
            Viewport v = (Viewport)iterator.next();
            BufferedImage img = v.getSqView().getImage();
            Rectangle b = v.getPixRect();

            g.drawImage(img, b.x, b.y, b.width, b.height, v.getSqView().getBackground(), null);
        }
        return;
    }

What this code does is get the image (stored in img) and get the pixel rectangle it needs to fit in (stored in b), then draw it in the space alloted via drawImage.

drawImage says that it scales images on the fly – which means that all images are being rescaled every frame. But the window is only resized rarely, so this must waste lots of processor time doing the same thing over and over again.

Now I saw this and decided that I would just update all the images upon resizing once, then store the result and be able to draw normally.

Like this:

public void resizeImage(int width, int height){
         BufferedImage resized = new BufferedImage(width, height, img.getType());
        Graphics2D g = resized.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(img, 0, 0, width, height, 0, 0, img.getWidth(), img.getHeight(), null);
        g.dispose();
        img = resized;
    }

This doesn’t work – I think it has something to do with img = resized. I just lose all the images with this code.

I have a few questions then.

  1. What is the performance cost of repeatedly scaling with drawImage? Is it any different even if the window has not been resized in between frames?
  2. How should I get the second code snippet to work? What is going wrong?
  3. If I apply a lighting filter to a tile, will that eat up tons of processor time as well if I run it each frame? (Think 225 or so small images on a 800×800 or so display)
  4. What is best practice for applying lighting filters? I am planning on overlaying on the whole map a pitch black filter, then exposing the areas around light sources.

Thanks for any help with this!

  • 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-17T18:26:41+00:00Added an answer on June 17, 2026 at 6:26 pm

    What is the performance cost of repeatedly scaling with drawImage? Is
    it any different even if the window has not been resized in between
    frames?

    You should always measure, but there is definitely a performance cost here, even if the window is not resized, because as the Javadoc says, there is no caching behind this drawImage method. The cost also depends on the frame rate.

    How should I get the second code snippet to work? What is going wrong?

    The second code snippet should be OK, I think the problem is somewhere else. Try reproducing the problem in a “small but complete” program, and post another question if you still see the problem.

    If I apply a lighting filter to a tile, will that eat up tons of processor time as well if I run it each frame? (Think 225 or so small images on a 800×800 or so display)

    You should always measure 🙂

    What is best practice for applying lighting filters? I am planning on overlaying on the whole map a pitch black filter, then exposing the areas around light sources.

    You can use an AlphaComposite for this.

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

Sidebar

Related Questions

I'm making a game where all movement is grid based. I also wan't to
I am making a grid-based game that will be much larger than the screen,
I am making a turn based hex-grid game. The player selects units and moves
I'm making a new game that generates a huge grid (lets say 1000x1000). The
I'm making a 2D grid based game in Qt. When clicking on a square
I'm making a grid-based game where you have land and water and the land
Making game of life I need to a have a grid that is 30x20
I'm making a program that draws a grid of boxes and the color of
I'm making a game that involves a 4*4 grid. The user enters letters and
If I'm making a simple grid based game, for example, I might have a

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.