001 package com.croftsoft.core.animation.updater; 002 003 import java.awt.Rectangle; 004 import javax.swing.JComponent; 005 006 import com.croftsoft.core.lang.NullArgumentException; 007 import com.croftsoft.core.animation.*; 008 import com.croftsoft.core.animation.sprite.TextSprite; 009 010 /********************************************************************* 011 * Slides text from one side to the other and then wraps back again. 012 * 013 * @version 014 * 2002-03-23 015 * @since 016 * 2002-02-22 017 * @author 018 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 019 *********************************************************************/ 020 021 public final class TextWrapUpdater 022 implements ComponentUpdater 023 ////////////////////////////////////////////////////////////////////// 024 ////////////////////////////////////////////////////////////////////// 025 { 026 027 private final TextSprite textSprite; 028 029 private final int deltaX; 030 031 private final int deltaY; 032 033 private final Rectangle textWrapArea; 034 035 private final boolean useComponentBounds; 036 037 private final Rectangle paintBounds; 038 039 private final Rectangle newPaintBounds; 040 041 ////////////////////////////////////////////////////////////////////// 042 ////////////////////////////////////////////////////////////////////// 043 044 /********************************************************************* 045 * @param textWrapArea 046 * 047 * If the textWrapArea is null, component.getBounds() will be used. 048 *********************************************************************/ 049 public TextWrapUpdater ( 050 TextSprite textSprite, 051 int deltaX, 052 int deltaY, 053 Rectangle textWrapArea ) 054 ////////////////////////////////////////////////////////////////////// 055 { 056 NullArgumentException.check ( this.textSprite = textSprite ); 057 058 this.deltaX = deltaX; 059 060 this.deltaY = deltaY; 061 062 if ( textWrapArea == null ) 063 { 064 this.textWrapArea = new Rectangle ( ); 065 066 useComponentBounds = true; 067 } 068 else 069 { 070 this.textWrapArea = textWrapArea; 071 072 useComponentBounds = false; 073 } 074 075 paintBounds = new Rectangle ( ); 076 077 newPaintBounds = new Rectangle ( ); 078 } 079 080 ////////////////////////////////////////////////////////////////////// 081 ////////////////////////////////////////////////////////////////////// 082 083 public void update ( JComponent component ) 084 ////////////////////////////////////////////////////////////////////// 085 { 086 if ( ( deltaX == 0 ) 087 && ( deltaY == 0 ) ) 088 { 089 return; 090 } 091 092 if ( useComponentBounds ) 093 { 094 component.getBounds ( textWrapArea ); 095 } 096 097 textSprite.getPaintBounds ( paintBounds ); 098 099 int x = paintBounds.x + deltaX; 100 101 int y = paintBounds.y + deltaY; 102 103 boolean wrapped = false; 104 105 if ( x <= textWrapArea.x - paintBounds.width ) 106 { 107 x = textWrapArea.x + textWrapArea.width - 1; 108 109 wrapped = true; 110 } 111 else if ( x >= textWrapArea.x + textWrapArea.width ) 112 { 113 x = textWrapArea.x - paintBounds.width + 1; 114 115 wrapped = true; 116 } 117 118 if ( y <= textWrapArea.y - paintBounds.height ) 119 { 120 y = textWrapArea.y + textWrapArea.height + paintBounds.height - 1; 121 122 wrapped = true; 123 } 124 else if ( 125 y >= textWrapArea.y + textWrapArea.height + paintBounds.height ) 126 { 127 y = textWrapArea.y - paintBounds.height + 1; 128 129 wrapped = true; 130 } 131 132 textSprite.setX ( x + textSprite.getX ( ) - paintBounds.x ); 133 134 textSprite.setY ( y + textSprite.getY ( ) - paintBounds.y ); 135 136 if ( !wrapped ) 137 { 138 textSprite.getPaintBounds ( newPaintBounds ); 139 140 paintBounds.add ( newPaintBounds ); 141 } 142 143 component.repaint ( paintBounds ); 144 } 145 146 ////////////////////////////////////////////////////////////////////// 147 ////////////////////////////////////////////////////////////////////// 148 }