001 package com.croftsoft.core.animation.icon; 002 003 import java.awt.Component; 004 import java.awt.Graphics; 005 import java.awt.Graphics2D; 006 import java.awt.Image; 007 import java.awt.image.VolatileImage; 008 import java.net.URL; 009 import javax.swing.Icon; 010 import javax.swing.ImageIcon; 011 012 import com.croftsoft.core.lang.NullArgumentException; 013 014 /********************************************************************* 015 * An Icon backed by a VolatileImage. 016 * 017 * @see 018 * VolatileImage 019 * 020 * @version 021 * 2002-03-13 022 * @since 023 * 2002-03-13 024 * @author 025 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 026 *********************************************************************/ 027 028 public final class VolatileImageIcon 029 implements Icon 030 ////////////////////////////////////////////////////////////////////// 031 ////////////////////////////////////////////////////////////////////// 032 { 033 034 private final Component component; 035 036 private final Image image; 037 038 // 039 040 private VolatileImage volatileImage; 041 042 ////////////////////////////////////////////////////////////////////// 043 ////////////////////////////////////////////////////////////////////// 044 045 public VolatileImageIcon ( 046 Component component, 047 Image image ) 048 ////////////////////////////////////////////////////////////////////// 049 { 050 NullArgumentException.check ( this.component = component ); 051 052 NullArgumentException.check ( this.image = image ); 053 } 054 055 ////////////////////////////////////////////////////////////////////// 056 ////////////////////////////////////////////////////////////////////// 057 058 public int getIconWidth ( ) { return image.getWidth ( null ); } 059 060 public int getIconHeight ( ) { return image.getHeight ( null ); } 061 062 ////////////////////////////////////////////////////////////////////// 063 ////////////////////////////////////////////////////////////////////// 064 065 public void paintIcon ( 066 Component component, 067 Graphics graphics, 068 int x, 069 int y ) 070 ////////////////////////////////////////////////////////////////////// 071 { 072 if ( ( volatileImage == null ) 073 || volatileImage.contentsLost ( ) ) 074 { 075 prepareVolatileImage ( ); 076 077 if ( volatileImage.contentsLost ( ) ) 078 { 079 graphics.drawImage ( image, x, y, null ); 080 } 081 } 082 083 graphics.drawImage ( volatileImage, x, y, component ); 084 } 085 086 ////////////////////////////////////////////////////////////////////// 087 ////////////////////////////////////////////////////////////////////// 088 089 private void prepareVolatileImage ( ) 090 ////////////////////////////////////////////////////////////////////// 091 { 092 if ( ( volatileImage == null ) 093 || ( volatileImage.validate ( 094 component.getGraphicsConfiguration ( ) ) 095 == VolatileImage.IMAGE_INCOMPATIBLE ) ) 096 { 097 volatileImage = component.createVolatileImage ( 098 image.getWidth ( null ), 099 image.getHeight ( null ) ); 100 } 101 102 Graphics2D graphics = volatileImage.createGraphics ( ); 103 104 graphics.drawImage ( image, 0, 0, component ); 105 106 graphics.dispose ( ); 107 } 108 109 ////////////////////////////////////////////////////////////////////// 110 ////////////////////////////////////////////////////////////////////// 111 }