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

后端 未结 7 1556
情话喂你
情话喂你 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 08:51

    How are you judging the computers' power? A 50x25 K 32-bit image takes more than 4.5 GB RAM to hold in memory (50000 * 25000 * 4 bytes). If one computer has more RAM than another, that can make a huge difference in speed, because it won't have to swap to disk as often. You should consider grabbing subsections of the image and working with those, instead of the whole thing.

    Edit: Are you using the latest Java & graphics drivers? If your image is only 5Kx2.5K, the only thing I can think of is that it's doing it without any hardware acceleration.

    0 讨论(0)
  • 2020-12-08 08:58

    There are several things that could influence performance here:

    • Available RAM
    • CPU speed
    • Graphic card (onboard or seperate)
    • Graphic driver
    • Java version
    • Used video mode (resolution, bitdepth, acceleration support)

    EDIT: Having a look at the edited question, I'd propose to check if the 9600GS system has the newest NVIDIA drivers installed. I recently installed a driver for an Intel onboard graphics card that replaced the generic Windows driver and made moving windows, watching videos, browsing etc. a lot faster.

    All the other specs look good. Perhaps Java doesn't detect the 9600GS and doesn't use hardware acceleration, but I doubt this.

    Also check the OS configuration. On Windows, you can turn off hardware acceleration for debugging purposes.

    Of course the best way to handle this would be to change your code - resize the image or split it up into chunks as DNS proposed. You'll never be able to see the whole image as it is on the screen.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 09:03

    Since Java uses OpenGL to do 2D drawing, the performance of your app will be affected by the OpenGL performance of the graphics chip in the respective computer. Support for OpenGL is dwindling in the 3D industry, which means that (ironically) newer chips may be slower at OpenGL rendering than older ones - not only due to hardware but also drivers.

    0 讨论(0)
  • 2020-12-08 09:04

    Check the screen settings. My bet is that pixel depth is different on the two systems, and that the slow one has an odd pixel depth related to the image object you are trying to display.

    0 讨论(0)
  • 2020-12-08 09:06

    If you are using sun's Java try some of the following system properties, either as command line parameters or the first lines in main

    sun.java2d.opengl=true  //force ogl  
    sun.java2d.ddscale=true //only when using direct3d  
    sun.java2d.translaccel=true //only when using direct3d  
    

    more flags can be viewed at this page

    Look at sun.java2d.trace which can allow you to determine the source of less-than-desirable graphics performance.

    0 讨论(0)
提交回复
热议问题