Slow Java2D Drawing on Integrated Graphics

China☆狼群 提交于 2019-12-20 06:31:33

问题


I'm working on a simple 2D game, rendering via the Java2D API. I've noticed that when I try to draw on integrated graphics card the performance crashes.

I've tested this game on both my main rig with a newer ATI Radeon and my 5 year old laptop which also has an (incredibly antiquated) Radeon. On both I get good FPS, but when I try to use my Intel i5's onboard HD 4000 graphics, it crawls at around 20 FPS.

I'm using Full Screen Exclusive mode.

At any given moment, I am rendering approximately 1000 images at once.

Annoyingly, when I try to getAvailableAcceleratedMemory() it just returns -1 for this card, and it seems to refuse to accelerate any images.

Does anyone have any ideas how to fix this issue?

Rendering code:

    Graphics g = bufferStrategy.getDrawGraphics();
    g.drawImage(img, x, y, img.getWidth(), img.getHeight(), null)
    g.dispose();
    bufferStrategy.show();

Image Loading code:

    BufferedImage I = null;
    I = ImageIO.read(new File(currentFolder+imgPath));
    imgMap.put(imgIdentifier, I);

The images are stored in a hashmap of BufferedImages identified by strings, so when an entity needs to draw and image it just gets it out of the hashmap and draws it. In the current case, the entities are mostly floor and wall tiles, so they never change (and thus don't have to get the image from the hashmap other than the very first time).

EDIT - I've incorporated MadProgrammer's method, but it didn't change my FPS.


回答1:


This is an example of converting an image to a compatiable image...not an answer in of itself

This is some of the library code that I use...

public static BufferedImage createCompatibleImage(BufferedImage image) {
    BufferedImage target = createCompatibleImage(image, image.getWidth(), image.getHeight());
    Graphics2D g2d = target.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    return target;
}

public static BufferedImage createCompatibleImage(BufferedImage image,
        int width, int height) {
    return getGraphicsConfiguration().createCompatibleImage(width, height, image.getTransparency());
}

public static GraphicsConfiguration getGraphicsConfiguration() {
    return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}

I would do something like...

I = createCompatibleImage(ImageIO.read(new File(currentFolder+imgPath)));
imgMap.put(imgIdentifier, I);


来源:https://stackoverflow.com/questions/19486459/slow-java2d-drawing-on-integrated-graphics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!