001 package com.croftsoft.apps.infant; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import javax.swing.*; 006 007 import com.croftsoft.core.animation.painter.*; 008 import com.croftsoft.core.lang.NullArgumentException; 009 010 /********************************************************************* 011 * Animated Swing component. 012 * 013 * @version 014 * $Date: 2007/02/25 03:17:05 $ 015 * @since 016 * 2005-08-12 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class InfantComponent 022 extends JComponent 023 ////////////////////////////////////////////////////////////////////// 024 ////////////////////////////////////////////////////////////////////// 025 { 026 027 private static final long serialVersionUID = 0L; 028 029 private static final ColorPainter BACKGROUND_COLOR_PAINTER 030 = new ColorPainter ( Color.WHITE ); 031 032 // 033 034 private final InfantAccessor infantAccessor; 035 036 private final ScalePainter scalePainter; 037 038 ////////////////////////////////////////////////////////////////////// 039 // constructor methods 040 ////////////////////////////////////////////////////////////////////// 041 042 /********************************************************************* 043 * Main constructor. 044 *********************************************************************/ 045 public InfantComponent ( 046 final InfantConfig infantConfig, 047 final InfantAccessor infantAccessor ) 048 ////////////////////////////////////////////////////////////////////// 049 { 050 NullArgumentException.checkArgs ( 051 infantConfig, 052 this.infantAccessor = infantAccessor ); 053 054 setOpaque ( true ); 055 056 setBackground ( infantConfig.getBackgroundColor ( ) ); 057 058 setForeground ( infantConfig.getForegroundColor ( ) ); 059 060 setFont ( infantConfig.getFont ( ) ); 061 062 setCursor ( infantConfig.getCursor ( ) ); 063 064 requestFocus ( ); 065 066 scalePainter = new ScalePainter ( ); 067 068 addComponentListener ( 069 new ComponentAdapter ( ) 070 { 071 @Override 072 public void componentResized ( ComponentEvent componentEvent ) 073 { 074 setImage ( scalePainter.getImage ( ) ); 075 } 076 } ); 077 } 078 079 ////////////////////////////////////////////////////////////////////// 080 ////////////////////////////////////////////////////////////////////// 081 082 public void setImage ( final Image image ) 083 ////////////////////////////////////////////////////////////////////// 084 { 085 scalePainter.setImage ( image ); 086 087 paintImmediately ( getVisibleRect ( ) ); 088 } 089 090 ////////////////////////////////////////////////////////////////////// 091 // overridden JComponent methods 092 ////////////////////////////////////////////////////////////////////// 093 094 @Override 095 public void paintComponent ( final Graphics graphics ) 096 ////////////////////////////////////////////////////////////////////// 097 { 098 final Graphics2D graphics2D = ( Graphics2D ) graphics; 099 100 BACKGROUND_COLOR_PAINTER.paint ( this, graphics2D ); 101 102 scalePainter.setScale ( infantAccessor.getScale ( ) ); 103 104 scalePainter.paint ( this, graphics2D ); 105 } 106 107 ////////////////////////////////////////////////////////////////////// 108 ////////////////////////////////////////////////////////////////////// 109 }