diff options
author | Timothée Floure | 2016-05-02 13:52:00 +0200 |
---|---|---|
committer | Timothée Floure | 2016-05-02 13:52:00 +0200 |
commit | 7e11b5a58a9f34049a5078139446ca080ff9b50b (patch) | |
tree | 0b37ee8b949ad2fea4544e99c20fd31d37745568 /src/ch/epfl | |
parent | da02a1baebad3ec350694ee097d9fc25612d8bdc (diff) | |
download | xblast-7e11b5a58a9f34049a5078139446ca080ff9b50b.tar.gz |
Implement client.ImageCollection
Diffstat (limited to 'src/ch/epfl')
-rw-r--r-- | src/ch/epfl/xblast/client/ImageCollection.java | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/client/ImageCollection.java b/src/ch/epfl/xblast/client/ImageCollection.java new file mode 100644 index 0000000..231d84c --- /dev/null +++ b/src/ch/epfl/xblast/client/ImageCollection.java | |||
@@ -0,0 +1,91 @@ | |||
1 | package ch.epfl.xblast.client; | ||
2 | |||
3 | import javax.imageio.ImageIO; | ||
4 | import java.util.HashMap; | ||
5 | import java.util.Map; | ||
6 | import java.awt.Image; | ||
7 | import java.io.File; | ||
8 | import java.util.NoSuchElementException; | ||
9 | |||
10 | /** | ||
11 | * @author Timothée FLOURE (257420) | ||
12 | */ | ||
13 | public final class ImageCollection { | ||
14 | |||
15 | /** | ||
16 | * Relative directory. | ||
17 | */ | ||
18 | private final String directory; | ||
19 | |||
20 | /** | ||
21 | * Get the directory Object from the relative directory Name using Java Resource. | ||
22 | * | ||
23 | * @param directoryName the relative name of the directory | ||
24 | * @throws NoSuchElementException | ||
25 | * @return the File Object related to the directory | ||
26 | */ | ||
27 | private static File getDirectory(String directoryName) { | ||
28 | try { // Ugly! Probably a better way... | ||
29 | return new File(ImageCollection.class | ||
30 | .getClassLoader() | ||
31 | .getResource(directoryName) | ||
32 | .toURI()); | ||
33 | } catch (java.net.URISyntaxException e) { | ||
34 | throw new NoSuchElementException(); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * Maps the directory's files with IDs parsed from their names. | ||
40 | * | ||
41 | * @return a map of id to files | ||
42 | */ | ||
43 | private Map<Integer, File> mapFilesId() { | ||
44 | HashMap<Integer, File> mappedFiles = new HashMap<>(); | ||
45 | |||
46 | for (File file : this.getDirectory(this.directory).listFiles()) { | ||
47 | mappedFiles.put(Integer.parseInt(file.getName()), file); | ||
48 | } | ||
49 | |||
50 | return mappedFiles; | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Instantiates a new ImageCollection. | ||
55 | * | ||
56 | * @param directory the relative directory name. | ||
57 | */ | ||
58 | public ImageCollection(String directory) { | ||
59 | this.directory = directory; | ||
60 | } | ||
61 | |||
62 | /** | ||
63 | * Returns the image corresponding to the given id. | ||
64 | * | ||
65 | * @throws java.util.NoSuchElementException if the requested image does not exists. | ||
66 | * @return the image corresponding to the given id | ||
67 | */ | ||
68 | public Image image(int id) { | ||
69 | if (this.imageOrNull(id) == null) | ||
70 | throw new NoSuchElementException(); | ||
71 | else | ||
72 | return this.imageOrNull(id); | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Returns the image corresponding to the given id. | ||
77 | * | ||
78 | * @return the image corresponding to the given id or null | ||
79 | */ | ||
80 | public Image imageOrNull(int id){ | ||
81 | // Ugly! Probably a better way... | ||
82 | if (mapFilesId().containsKey(id)) | ||
83 | try { | ||
84 | return ImageIO.read(mapFilesId().get(id)); | ||
85 | } catch (java.io.IOException e) { | ||
86 | return null; | ||
87 | } | ||
88 | else | ||
89 | return null; | ||
90 | } | ||
91 | } | ||