From 381662e697b8bf173f250a946813b2be173bf069 Mon Sep 17 00:00:00 2001 From: melvin Date: Mon, 25 Jul 2011 12:05:30 +0800 Subject: [PATCH] only store one copy of images --- .../data/HighQualityCardImagesProvider.java | 62 ++++++------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/src/magic/data/HighQualityCardImagesProvider.java b/src/magic/data/HighQualityCardImagesProvider.java index 454473250b..5c7c99aa55 100644 --- a/src/magic/data/HighQualityCardImagesProvider.java +++ b/src/magic/data/HighQualityCardImagesProvider.java @@ -19,18 +19,11 @@ import magic.model.MagicCardDefinition; public class HighQualityCardImagesProvider implements CardImagesProvider { private static final CardImagesProvider INSTANCE=new HighQualityCardImagesProvider(); - private static final int MAX_IMAGES=100; - - private final Map lowImagesMap; - private final Map highImagesMap; - private final LinkedList retrievedList; + private final Map imagesMap; public HighQualityCardImagesProvider() { - - lowImagesMap=new HashMap(); - highImagesMap=new HashMap(); - retrievedList=new LinkedList(); + imagesMap = new magic.ai.StateCache(100); } private static final String getFilename( @@ -47,18 +40,23 @@ public class HighQualityCardImagesProvider implements CardImagesProvider { } private static BufferedImage loadCardImage(final String filename) { - try { - final InputStream inputStream=new FileInputStream(filename); - final BufferedImage fullImage=ImageIO.read(inputStream); - inputStream.close(); - return fullImage; - } catch (final Exception ex) { + final File imageFile = new File(filename); + if (imageFile.isFile()) { + try { + final InputStream inputStream=new FileInputStream(imageFile); + final BufferedImage fullImage=ImageIO.read(inputStream); + inputStream.close(); + return fullImage; + } catch (Exception ex) { + return IconImages.MISSING_CARD; + } + } else { return IconImages.MISSING_CARD; } } @Override - public BufferedImage getImage( + public Image getImage( final MagicCardDefinition cardDefinition, final int index, final boolean high) { @@ -68,40 +66,20 @@ public class HighQualityCardImagesProvider implements CardImagesProvider { } final String filename=getFilename(cardDefinition,index); - final Map map=high?highImagesMap:lowImagesMap; - BufferedImage image=map.get(filename); + Image image = imagesMap.get(filename); if (image != null) { - retrievedList.remove(filename); - retrievedList.addLast(filename); return image; } - if (retrievedList.size() >= MAX_IMAGES) { - final String first=retrievedList.removeFirst(); - lowImagesMap.remove(first); - highImagesMap.remove(first); - } - - image=loadCardImage(filename); - BufferedImage lowImage; - if (image==IconImages.MISSING_CARD) { - lowImage=image; - } else { - final Image scaledImage=image.getScaledInstance( + image = loadCardImage(filename).getScaledInstance( CARD_WIDTH, CARD_HEIGHT, Image.SCALE_SMOOTH); - lowImage=new BufferedImage( - CARD_WIDTH, - CARD_HEIGHT, - BufferedImage.TYPE_INT_RGB); - lowImage.getGraphics().drawImage(scaledImage,0,0,null); - } - lowImagesMap.put(filename,lowImage); - highImagesMap.put(filename,image); - retrievedList.addLast(filename); - return high?image:lowImage; + + imagesMap.put(filename, image); + + return image; } public static CardImagesProvider getInstance() {