View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.util;
17  
18  import java.awt.Image;
19  import java.util.HashMap;
20  
21  import javax.swing.ImageIcon;
22  
23  /**
24   * An image cache to make loading of pages a lot quicker.
25   * At destroy of a part if will empty the cache, so the image
26   * resources are freed.
27   *
28   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
29   * @version $Id: ImageCache.java,v 1.1 2005/12/18 12:58:22 mvdb Exp $
30   */
31  public class ImageCache {
32  
33      private HashMap images;
34  
35      /**
36       * 
37       */
38      public ImageCache() {
39          images = new HashMap();
40      }
41      
42      public void addImage(String name, Image image) {
43          images.put(name, image);
44      }
45      
46      public void addImage(String name, ImageIcon image) {
47          images.put(name, image);
48      }
49      
50      public Image getImage(String name) {
51          Object img = images.get(name);
52          if (img instanceof ImageIcon) {
53              return ((ImageIcon) img).getImage();
54          } else {
55              return (Image) img;
56          }
57      }
58      
59      public ImageIcon getImageIcon(String name) {
60          Object img = images.get(name);
61          if (img instanceof Image) {
62              return new ImageIcon((Image)img);
63          } else {
64              return (ImageIcon) img;
65          }
66      }
67      
68      public void clear() {
69          images.clear();
70          images = null;
71      }
72  
73  }