summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/util/ObjectLoader.java
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-01-17 10:36:37 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-01-17 10:36:37 -0500
commit69b495c392bffe96dab97306a42466edd4b2474e (patch)
tree2adccc01f4027bcb76bbefee03bb6a9622ce3e00 /src/com/fourisland/fourpuzzle/util/ObjectLoader.java
parent4c768483aecd687529b9abb48ed42950fabc886f (diff)
downloadfourpuzzle-69b495c392bffe96dab97306a42466edd4b2474e.tar.gz
fourpuzzle-69b495c392bffe96dab97306a42466edd4b2474e.tar.bz2
fourpuzzle-69b495c392bffe96dab97306a42466edd4b2474e.zip
Imported sources
Diffstat (limited to 'src/com/fourisland/fourpuzzle/util/ObjectLoader.java')
-rw-r--r--src/com/fourisland/fourpuzzle/util/ObjectLoader.java92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/com/fourisland/fourpuzzle/util/ObjectLoader.java b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java new file mode 100644 index 0000000..c14a8a1 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java
@@ -0,0 +1,92 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package com.fourisland.fourpuzzle.util;
7
8//import com.alienfactory.javamappy.loader.MapLoader;
9import com.fourisland.fourpuzzle.PuzzleApplication;
10import java.awt.Image;
11import java.awt.Toolkit;
12import java.awt.image.BufferedImage;
13import java.awt.image.FilteredImageSource;
14import java.util.HashMap;
15import javax.imageio.ImageIO;
16import javax.sound.midi.MidiSystem;
17import javax.sound.midi.Sequence;
18import org.jdesktop.application.ResourceMap;
19
20/**
21 *
22 * @author hatkirby
23 */
24public class ObjectLoader {
25
26 private static HashMap<String,Object> objectCache = new HashMap<String,Object>();
27
28 public static BufferedImage getImage(String type, String name) throws Exception
29 {
30 if (!objectCache.containsKey(type + "/" + name))
31 {
32 ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap();
33 String filename = rm.getResourcesDir() + type.toLowerCase() + "/" + name + ".png";
34 BufferedImage bImg = ImageIO.read(rm.getClassLoader().getResourceAsStream(filename));
35
36 addToObjectCache(type,name,bImg);
37 }
38
39 return (BufferedImage) objectCache.get(type + "/" + name);
40 }
41
42 public static BufferedImage getImage(String type, String name, int transparencyColor) throws Exception
43 {
44 if (!objectCache.containsKey(type + "/" + name))
45 {
46 ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap();
47 String filename = rm.getResourcesDir() + type + "/" + name + ".png";
48 BufferedImage bImg = ImageIO.read(rm.getClassLoader().getResourceAsStream(filename));
49 bImg = new BufferedImage(bImg.getWidth(), bImg.getHeight(), BufferedImage.TYPE_INT_RGB);
50 Image image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(bImg.getSource(), new TransparentImageFilter(transparencyColor)));
51 bImg.createGraphics().drawImage(image, 0, 0, null);
52
53 addToObjectCache(type,name,bImg);
54 }
55
56 return (BufferedImage) objectCache.get(type + "/" + name);
57 }
58
59 /*public static com.alienfactory.javamappy.Map getMap(String name) throws Exception
60 {
61 ResourceMap rm = PuzzleApplication.getInstance().getContext().getResourceManager().getResourceMap();
62 String filename = rm.getResourcesDir() + "mapdata/" + name + ".fmp";
63
64 //com.alienfactory.javamappy.Map map = MapLoader.loadMap(rm.getClassLoader().getResourceAsStream(filename));
65 return map;
66 }*/
67
68 public static void addToObjectCache(String type, String name, Object object) throws Exception
69 {
70 if (objectCache.size() >= 100)
71 {
72 objectCache.clear();
73 }
74
75 objectCache.put(type + "/" + name, object);
76 }
77
78 public static Sequence getMusic(String name) throws Exception
79 {
80 if (!objectCache.containsKey("Music/" + name))
81 {
82 ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap();
83 String filename = rm.getResourcesDir() + "music/" + name + ".mid";
84 Sequence seq = MidiSystem.getSequence(rm.getClassLoader().getResourceAsStream(filename));
85
86 addToObjectCache("Music", name, seq);
87 }
88
89 return (Sequence) objectCache.get("Music/" + name);
90 }
91
92}