001 package com.croftsoft.core.animation.icon; 002 003 import java.awt.*; 004 import java.awt.image.BufferedImage; 005 import java.io.*; 006 import javax.swing.Icon; 007 008 import com.croftsoft.core.awt.image.ImageCache; 009 import com.croftsoft.core.lang.NullArgumentException; 010 011 /********************************************************************* 012 * An Icon that uses an ImageCache. 013 * 014 * @version 015 * 2003-03-17 016 * @since 017 * 2003-03-17 018 * @author 019 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 020 *********************************************************************/ 021 022 public final class ImageCacheIcon 023 implements Icon 024 ////////////////////////////////////////////////////////////////////// 025 ////////////////////////////////////////////////////////////////////// 026 { 027 028 private final ImageCache imageCache; 029 030 private final String imageFilename; 031 032 // 033 034 private BufferedImage bufferedImage; 035 036 ////////////////////////////////////////////////////////////////////// 037 ////////////////////////////////////////////////////////////////////// 038 039 public ImageCacheIcon ( 040 ImageCache imageCache, 041 String imageFilename ) 042 ////////////////////////////////////////////////////////////////////// 043 { 044 NullArgumentException.check ( this.imageCache = imageCache ); 045 046 NullArgumentException.check ( this.imageFilename = imageFilename ); 047 } 048 049 ////////////////////////////////////////////////////////////////////// 050 ////////////////////////////////////////////////////////////////////// 051 052 public int getIconWidth ( ) 053 ////////////////////////////////////////////////////////////////////// 054 { 055 return getBufferedImage ( ).getWidth ( ); 056 } 057 058 public int getIconHeight ( ) 059 ////////////////////////////////////////////////////////////////////// 060 { 061 return getBufferedImage ( ).getHeight ( ); 062 } 063 064 ////////////////////////////////////////////////////////////////////// 065 ////////////////////////////////////////////////////////////////////// 066 067 public void paintIcon ( 068 Component component, 069 Graphics graphics, 070 int x, 071 int y ) 072 ////////////////////////////////////////////////////////////////////// 073 { 074 graphics.drawImage ( getBufferedImage ( ), x, y, null ); 075 } 076 077 ////////////////////////////////////////////////////////////////////// 078 ////////////////////////////////////////////////////////////////////// 079 080 private BufferedImage getBufferedImage ( ) 081 ////////////////////////////////////////////////////////////////////// 082 { 083 if ( bufferedImage == null ) 084 { 085 try 086 { 087 bufferedImage = imageCache.get ( imageFilename ); 088 } 089 catch ( IOException ex ) 090 { 091 throw new RuntimeException ( ex ); 092 } 093 } 094 095 return bufferedImage; 096 } 097 098 ////////////////////////////////////////////////////////////////////// 099 ////////////////////////////////////////////////////////////////////// 100 }