001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package examples.cityguide;
033:
034: import java.io.IOException;
035:
036: import java.util.Enumeration;
037: import java.util.Hashtable;
038:
039: import javax.microedition.lcdui.Image;
040:
041: /**
042: * Lets you load image from resource and stores it in cache for
043: * later usage
044: */
045: public class ImageManager {
046: private static ImageManager im = null;
047: private static Hashtable imageCache = null;
048:
049: private ImageManager() {
050: imageCache = new Hashtable();
051: }
052:
053: public static ImageManager getInstance() {
054: if (im == null) {
055: im = new ImageManager();
056: }
057:
058: return im;
059: }
060:
061: /**
062: * Load image from resource and store it
063: * in cache.
064: */
065: public Image getImage(String name) {
066: Image image = null;
067:
068: try {
069: if (null == (image = (Image) imageCache.get(name))) {
070: image = Image.createImage("/" + name + ".png");
071: imageCache.put(name, image);
072: }
073: } catch (IOException ioe) {
074: ioe.printStackTrace();
075: }
076:
077: return image;
078: }
079:
080: /**
081: * Batch load images into cache
082: */
083: public void loadImagesCache(String[] names) {
084: for (int i = 0; i < names.length; i++) {
085: try {
086: if (names[i] != null) {
087: imageCache.put(names[i], Image.createImage("/"
088: + names[i] + ".png"));
089: }
090: } catch (IOException ioe) {
091: ioe.printStackTrace();
092: }
093: }
094: }
095:
096: /**
097: * Batch load images into cache
098: */
099: public void loadImagesCache(Enumeration e) {
100: for (; e.hasMoreElements();) {
101: try {
102: String name = (String) e.nextElement();
103: imageCache.put(name, Image.createImage("/" + name
104: + ".png"));
105: } catch (IOException ioe) {
106: ioe.printStackTrace();
107: }
108: }
109: }
110: }
|