Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  How to have BufferedImages take up less RAM  (Read 12037 times)
0 Members and 1 Guest are viewing this topic.
Offline mrexiled

Senior Newbie





« Reply #30 - Posted 2018-03-15 02:55:41 »

Hello! As the title says, BufferedImages take up the majority of the RAM in my Java game. I use mostly pngs; some of which are transparent.

I initiate the BufferedImage objects simply like so:

1  
BufferedImage example = ImageIO.read(Runner.class.getResource("/exampleImage.png"));


Then, I use a getCompatibleImage() function to make it the best image for rendering on the system.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
static BufferedImage getCompatibleImage(BufferedImage current) {
    GraphicsConfiguration gfxConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    if(current.getColorModel().equals(gfxConfig.getColorModel()))
        return current;
    BufferedImage optimized = gfxConfig.createCompatibleImage(current.getWidth(), current.getHeight(), current.getTransparency());
    Graphics2D g2d = optimized.createGraphics();
    g2d.drawImage(current, 0, 0, null);
    optimized.setAccelerationPriority(1);
    return optimized;
}


I use about 1219 BufferedImage objects, which is quite a lot. I read all of them at the beginning with a loading screen splash image and keep all of them as static b/c I do not want to reload them on a restart.

Please let me know what I can do to use less RAM. Thanks!


have you tried storing buffered images temporarily?
because a spritesheet is ONE image but it will be cropped into multiple buffered images right?

if thats the case try using something similar to this method to load a image.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public static Image getSprite(int id) {
    BufferedImage tempSprite; //isn't stored constantly?

    try {
        tempSprite = ImageIO.read(new File("spriteFolder/" + id + ".png);
       
    }
    catch(IOException e) {

    }

   return tempSprite;

}


i've used this way of loading images with the 'drawImage' method and it increased my performance alot. (still not sure if its the BEST way though)

1  
drawImage(getSprite(2), 0, 0, 50, 50, null); 





Are you an ambitious java developer that wants to generate some revenue?
Click here for an interview
Offline ddyer
« Reply #31 - Posted 2018-03-15 19:00:16 »

FWIW.

There is such a wide range of screen resolutions in use, it's not practical to have appropriate-size
images for everything.   I use a process which starts with an oversize image, and dynamically resizes
down to an appropriate size using smooth scaling.  I could (but don't) also discard the original oversize
image.   Two things are important to make this work; the oversize images shouldn't have hard edges so
the scaled versions will look decent, and the UI code needs to use an container class instead of raw
images to hide all the machinery.


Pages: 1 [2]
  ignore  |  Print  
 
 

 
hadezbladez (377 views)
2018-11-16 13:46:03

hadezbladez (198 views)
2018-11-16 13:41:33

hadezbladez (379 views)
2018-11-16 13:35:35

hadezbladez (93 views)
2018-11-16 13:32:03

EgonOlsen (2195 views)
2018-06-10 19:43:48

EgonOlsen (2235 views)
2018-06-10 19:43:44

EgonOlsen (1387 views)
2018-06-10 19:43:20

DesertCoockie (2025 views)
2018-05-13 18:23:11

nelsongames (1688 views)
2018-04-24 18:15:36

nelsongames (2322 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!