Search code examples
javaimageawttoolkit

difference between getImage and createImage in Toolkit


Toolkit class has two methods, createImage() and getImage(), which both return an Image.

What is the difference between the two and when to use which one?


Solution

  • Image createImage(URL url)<br/>
    Image getImage(URL url)<br/>
    

    These two methods from the Toolkit class
    http://docs.oracle.com/javase/7/docs/api/java/awt/Toolkit.html
    are similar.
    createImage() leaves you the full control over the created image, which means you have to be carefull you don't call it too often for the same image, or else you're going to slow your application and consume too much memory,
    whereas using getImage() many calls for the same image won't result in a high memory consumption and time lost fetching the image on the remote URL. When you use getImage() you rely on the Toolkit that will deal with the caching.