| 91 | There is a method that multiplies the image pixels by a color in ElementHelper, it will be copied here and removed from ElementHelper. |
| 92 | |
| 93 | '''Caching recently loaded ImmImages''' will prevent us from loading the same image more than once. |
| 94 | * All constructors except the one with BufferedImage will be deleted or made private. |
| 95 | * A new method will be created instead of them: |
| 96 | |
| 97 | {{{ |
| 98 | /** |
| 99 | * Loads an ImmImage from the given URL. |
| 100 | * @param url |
| 101 | * The URL from which to load the image. |
| 102 | * @return |
| 103 | * The loaded image |
| 104 | */ |
| 105 | public static ImmImage loadCached(URL url) { |
| 106 | if (!loadedImages.containsKey(url)) { |
| 107 | try { |
| 108 | InputStream is = null; |
| 109 | try { |
| 110 | is = url.openStream(); |
| 111 | loadedImages.put(url, new ImmImage(is)); |
| 112 | } finally { |
| 113 | if (is != null) { |
| 114 | is.close(); |
| 115 | } |
| 116 | } |
| 117 | } catch (IOException e) { |
| 118 | throw new RuntimeException(e); |
| 119 | } |
| 120 | } |
| 121 | return loadedImages.get(url); |
| 122 | } |
| 123 | }}} |
| 124 | |
| 125 | It is a good idea to create a new class representing a map which keeps weak/soft references to its values. Otherwise we could run out of memory. [[BR]] |
| 126 | However most probably this won't be done now. |