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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:17:34+00:00 2026-06-14T17:17:34+00:00

I am implementing a board game where each player has several units and each

  • 0

I am implementing a board game where each player has several units and each unit is shown to a user using a common image which I read from a file using the following method. I read this image at application startup and going to use it later.

    private static BufferedImage readBufferedImage (String imagePath) {
        try {
            InputStream is = IconManager.class.getClassLoader().getResourceAsStream(imagePath);
            BufferedImage bimage = ImageIO.read(is);
            is.close();
            return bimage;
        } catch (Exception e) {
            return null;
        }
    }

Units differ with various colorful tokens located on the top of that common image.

Before I just add several commonImages to JPanel and tokens were implemented using JLabels which were floating on the top of commonImage

    //at startup
    ImageIcon commonImage = new ImageIcon(readBufferedImage("image.png"));
    ...
    JPanel panel = new JPanel();
    panel.add(commonImage);
    panel.add(commonImage);

    //located JLabels with token on the top of each commonImage        

However, now I want to use JScrollPane instead of JPanel, so I think it is a better approach to drawString() and drawImage() to each commonImage before I show it to a user.

I estimate roughly a number of units as 20. So now every turn for every unit I would need to generate on-the-fly separate BufferedImage with various tokens configuration.

The question is whether I should cache already generated BufferedImages depending on token configuration to extract from cache if the image has been generated before with same configuration?

  • 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-14T17:17:36+00:00Added an answer on June 14, 2026 at 5:17 pm

    The question you ask depends on a few factors:

    1. How long it takes to generate the image from scratch
    2. How long it takes to check if the image was generated before
    3. How large each image will be
    4. The probability of the image being reused

    So without having a good knowledge of your application, no one is going to be able to give you an answer accurate for every situation.

    In general, if you’re only moving tokens around, it should be quick to implement an update and the paintComponent method of the panel your drawing. If you save your base image, token image and the current resulting image (basically what you proposed), I wouldn’t anticipate a performance problem. The key is to do your updating of the image in your custom update method, and always draw the current resulting image in the paintComponent method.

    Update

    For example, you can cache the current display using code similar to this:

    private BufferedImage cachedImage;
    ...
    
    @Override
    public void paintComponent(Graphics g){
        //If the image needs to be refreshed draw it to the cache first
        if(cachedImage == null){
            cachedImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            super.paintComponent(cachedImage.getGraphics());
        }
    
        //Draw the image from cache
        g.drawImage(cachedImage, 0, 0, this);
    }
    

    When you want to clear the cache, in your method that does the update, you’d set cachedImage = null;

    But in your situation,

    • Any performance problems at the scale of 200×200 you experience would have to do with the number of drawings you’re doing. Since I’m guessing you’re not concerned about a frames/second rate, I doubt you’ll have performance problems (you can collect timings to see exactly how long it takes to draw – I’d guess well under 100 ms)
    • The easiest thing to do would be cache the images of the tokens and their strings (I’m assuming they rarely change), but still dynamically create the layout of the tokens. (Any other performance gains in your situation aren’t going to be so easy).
    • Caching previous rendering of the screen will essentially break the swing display model (not much point in using it – just do the layout yourself). This actually applies to pretty much any caching of a container.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a simple board game (Breakthrough) using OpenGL (plus GLUT and GLUI).
I am implementing a basic board game in Java and that I am having
I am implementing a checkers game board with python. Here is how I generate
I am implementing a turn based game, there are two sides and each side
I was trying to implement the Sorry! board game using C++ such that 4
I'm implementing a board game for a course I'm taking. There are 5 racks
I'm implementing a card game. In this game I have a Board that is
I am implementing Othello game in Prolog. The game board is represented as list
I'm implementing a chess game in c++ and some of the classes are Board
I am implementing a game of go on a board size of 7 x

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.