Crossfire JXClient, Trunk  R20561
ImageParser.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.skin.io;
23 
27 import java.awt.Color;
28 import java.awt.Image;
29 import java.awt.image.BufferedImage;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import javax.imageio.ImageIO;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
41 public class ImageParser {
42 
46  @NotNull
48 
52  @NotNull
53  private final JXCSkinSource skinSource;
54 
59  public ImageParser(@NotNull final JXCSkinSource skinSource) {
60  this.skinSource = skinSource;
61  }
62 
66  public void clear() {
67  definedImages.clear();
68  }
69 
77  @Nullable
78  public Image getImage(@Nullable final Color color, @NotNull final String name) throws IOException {
79  //noinspection VariableNotUsedInsideIf
80  return color == null ? getImage(name) : null;
81  }
82 
89  @NotNull
90  public BufferedImage getImage(@NotNull final String name) throws IOException {
91  try {
92  return definedImages.lookup(name);
93  } catch (final JXCSkinException ignored) {
94  // ignore
95  }
96 
97  final String filename = "pictures/"+name+".png";
98  final BufferedImage image;
99  try (final InputStream inputStream = skinSource.getInputStream(filename)) {
100  image = ImageIO.read(inputStream);
101  }
102  if (image == null) {
103  throw new IOException("image '"+skinSource.getURI(filename)+"' does not exist");
104  }
105  try {
106  definedImages.insert(name, image);
107  } catch (final JXCSkinException ex) {
108  throw new AssertionError(ex);
109  }
110  return image;
111  }
112 
113 }
String getURI(@NotNull String name)
Returns a description of the location of a resource name.
InputStream getInputStream(@NotNull String name)
Returns an InputStream for a resource name.
Image getImage(@Nullable final Color color, @NotNull final String name)
Optionally loads an image by base file name.
void insert(@NotNull final String name, @NotNull final T t)
Adds a new element to the cache.
Interface for providers of JXCSkin sources.
ImageParser(@NotNull final JXCSkinSource skinSource)
Creates a new instance.
Implements a cache for elements identified by name.
Exception thrown if a skin related problem occurs.
void clear()
Forgets all defined images.
final JXCSkinCache< BufferedImage > definedImages
All defined images.
BufferedImage getImage(@NotNull final String name)
Loads an image by base file name.
T lookup(@NotNull final String name)
Looks up an element by name.
Creates BufferedImage instances from string representations.
final JXCSkinSource skinSource
The JXCSkinSource for loading resources.