Skip to content

Commit

Permalink
Add support for jpg and png file extensions for small map (#3452)
Browse files Browse the repository at this point in the history
* Add support for jpg and png file extensions for small map

* Fix error message
  • Loading branch information
ron-murhammer authored and RoiEXLab committed Jun 9, 2018
1 parent 4ed0cd8 commit 4e8c9e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Expand Up @@ -147,7 +147,8 @@ public interface Constants {
String UNIT_TYPE_AAGUN = "aaGun";
String UNIT_TYPE_ARTILLERY = "artillery";
String UNIT_TYPE_DESTROYER = "destroyer";
String SMALL_MAP_FILENAME = "smallMap.jpeg";
String SMALL_MAP_FILENAME = "smallMap";
String[] SMALL_MAP_EXTENSIONS = {"jpeg", "jpg", "png"};
String MAP_NAME = "mapName";
// new scramble property names
String SCRAMBLE_RULES_IN_EFFECT = "Scramble Rules In Effect";
Expand Down
32 changes: 20 additions & 12 deletions game-core/src/main/java/games/strategy/triplea/image/MapImage.java
Expand Up @@ -7,6 +7,7 @@
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.prefs.Preferences;

import javax.imageio.ImageIO;
Expand All @@ -20,17 +21,6 @@
* Is not responsible for drawing things on top of the map, such as units, routes etc.
*/
public class MapImage {
private static Image loadImage(final ResourceLoader loader, final String name) {
final URL mapFileUrl = loader.getResource(name);
if (mapFileUrl == null) {
throw new IllegalStateException("resource not found:" + name);
}
try {
return ImageIO.read(mapFileUrl);
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}

private BufferedImage smallMapImage;
private static Font propertyMapFont = null;
Expand Down Expand Up @@ -156,11 +146,29 @@ public BufferedImage getSmallMapImage() {
}

public void loadMaps(final ResourceLoader loader) {
final Image smallFromFile = loadImage(loader, Constants.SMALL_MAP_FILENAME);
final Image smallFromFile = loadImage(loader, Constants.SMALL_MAP_FILENAME, Constants.SMALL_MAP_EXTENSIONS);
smallMapImage = Util.createImage(smallFromFile.getWidth(null), smallFromFile.getHeight(null), false);
final Graphics g = smallMapImage.getGraphics();
g.drawImage(smallFromFile, 0, 0, null);
g.dispose();
smallFromFile.flush();
}

private static Image loadImage(final ResourceLoader loader, final String name, final String[] extensions) {
URL mapFileUrl = null;
for (final String extension : extensions) {
mapFileUrl = loader.getResource(name + "." + extension);
if (mapFileUrl != null) {
break;
}
}
if (mapFileUrl == null) {
throw new IllegalStateException("File not found: " + name + " with extensions: " + Arrays.toString(extensions));
}
try {
return ImageIO.read(mapFileUrl);
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}
}

0 comments on commit 4e8c9e1

Please sign in to comment.