added comments and shortened some lines
parent
3bfa412301
commit
3227c09080
|
@ -1,14 +1,19 @@
|
|||
package magic.data;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import magic.model.MagicCardDefinition;
|
||||
|
||||
/**
|
||||
* Interface for getting image of a card
|
||||
*/
|
||||
public interface CardImagesProvider {
|
||||
|
||||
public static final String IMAGE_EXTENSION=".jpg";
|
||||
public static final int CARD_WIDTH=203;
|
||||
public static final int CARD_HEIGHT=289;
|
||||
|
||||
public BufferedImage getImage(final MagicCardDefinition cardDefinition,final int index,final boolean high);
|
||||
}
|
||||
public BufferedImage getImage(
|
||||
final MagicCardDefinition cardDefinition,
|
||||
final int index,
|
||||
final boolean high);
|
||||
}
|
||||
|
|
|
@ -8,12 +8,14 @@ import java.io.InputStream;
|
|||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import magic.MagicMain;
|
||||
import magic.model.MagicCardDefinition;
|
||||
|
||||
/**
|
||||
* For a given MagicCardDefinition object returns the corresponding image from
|
||||
* the cards directory
|
||||
*/
|
||||
public class HighQualityCardImagesProvider implements CardImagesProvider {
|
||||
|
||||
private static final CardImagesProvider INSTANCE=new HighQualityCardImagesProvider();
|
||||
|
@ -31,22 +33,23 @@ public class HighQualityCardImagesProvider implements CardImagesProvider {
|
|||
retrievedList=new LinkedList<String>();
|
||||
}
|
||||
|
||||
private static final String getFilename(final MagicCardDefinition cardDefinition,final int index) {
|
||||
|
||||
private static final String getFilename(
|
||||
final MagicCardDefinition cardDefinition,
|
||||
final int index) {
|
||||
final int imageIndex=index%cardDefinition.getImageCount();
|
||||
final StringBuffer buffer=new StringBuffer();
|
||||
buffer.append(MagicMain.getGamePath()).append(File.separator);
|
||||
buffer.append(cardDefinition.isToken()?"tokens":"cards").append(File.separator);
|
||||
buffer.append(cardDefinition.getImageName()).append(imageIndex>0?String.valueOf(imageIndex+1):"");
|
||||
buffer.append(cardDefinition.getImageName())
|
||||
buffer.append(imageIndex>0?String.valueOf(imageIndex+1):"");
|
||||
buffer.append(IMAGE_EXTENSION);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
private static BufferedImage loadCardImage(final String filename) {
|
||||
|
||||
try {
|
||||
final InputStream inputStream=new FileInputStream(filename);
|
||||
BufferedImage fullImage=ImageIO.read(inputStream);
|
||||
final BufferedImage fullImage=ImageIO.read(inputStream);
|
||||
inputStream.close();
|
||||
return fullImage;
|
||||
} catch (final Exception ex) {
|
||||
|
@ -55,31 +58,44 @@ public class HighQualityCardImagesProvider implements CardImagesProvider {
|
|||
}
|
||||
|
||||
@Override
|
||||
public BufferedImage getImage(final MagicCardDefinition cardDefinition,final int index,final boolean high) {
|
||||
public BufferedImage getImage(
|
||||
final MagicCardDefinition cardDefinition,
|
||||
final int index,
|
||||
final boolean high) {
|
||||
|
||||
if (cardDefinition==null) {
|
||||
if (cardDefinition == null) {
|
||||
return IconImages.MISSING;
|
||||
}
|
||||
|
||||
final String filename=getFilename(cardDefinition,index);
|
||||
final Map<String,BufferedImage> map=high?highImagesMap:lowImagesMap;
|
||||
BufferedImage image=map.get(filename);
|
||||
if (image!=null) {
|
||||
|
||||
if (image != null) {
|
||||
retrievedList.remove(filename);
|
||||
retrievedList.addLast(filename);
|
||||
return image;
|
||||
}
|
||||
if (retrievedList.size()>=MAX_IMAGES) {
|
||||
|
||||
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) {
|
||||
lowImage=image;
|
||||
} else {
|
||||
final Image scaledImage=image.getScaledInstance(CARD_WIDTH,CARD_HEIGHT,Image.SCALE_SMOOTH);
|
||||
lowImage=new BufferedImage(CARD_WIDTH,CARD_HEIGHT,BufferedImage.TYPE_INT_RGB);
|
||||
final Image scaledImage=image.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);
|
||||
|
@ -89,7 +105,6 @@ public class HighQualityCardImagesProvider implements CardImagesProvider {
|
|||
}
|
||||
|
||||
public static CardImagesProvider getInstance() {
|
||||
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue