Graphics.drawImage() in Java is EXTREMELY slow on some computers yet much faster on others

后端 未结 7 1583
情话喂你
情话喂你 2020-12-08 08:22

I\'m having a strange problem, basically in Java Graphics.drawImage() is extremely slow on some computers and faster on others. This isn\'t related to the computers power ei

7条回答
  •  爱一瞬间的悲伤
    2020-12-08 09:03

    Performance of writing an image to a screen is very much affected by the format in which the image is stored. If the format is the same as the screen memory wants then it can be very fast; if it is not then a conversion must be done, sometimes pixel by pixel, which is very slow.

    If you have any control over how the image is stored, you should store it in a format that the screen is looking for. Here is some sample code:

        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        GraphicsConfiguration config = device.getDefaultConfiguration();
        BufferedImage buffy = config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        Graphics g = buffy.getGraphics();
    

    If you are going to draw the image many times it may be worth converting to a compatible format even if it came in some other format.

    Drawing an image will also be slower if you are transforming it as you draw, which the 'resizing' part of your description makes me think you might be. Again, do the resize once (when the window is resized) and cache the resized and compatible image so that it can be redrawn quickly.

提交回复
热议问题