001 package com.croftsoft.core.animation.component;
002
003 import java.awt.Graphics2D;
004 import java.awt.Rectangle;
005 import java.awt.geom.Rectangle2D;
006 import java.awt.image.BufferStrategy;
007
008 import com.croftsoft.core.animation.AnimatedComponent;
009 import com.croftsoft.core.animation.ComponentAnimator;
010 import com.croftsoft.core.animation.RepaintCollector;
011 import com.croftsoft.core.animation.factory.DefaultAnimationFactory;
012 import com.croftsoft.core.lang.NullArgumentException;
013 import com.croftsoft.core.util.loop.FixedDelayLoopGovernor;
014 import com.croftsoft.core.util.loop.LoopGovernor;
015
016 /*********************************************************************
017 * AnimatedComponent subclass that uses a BufferStrategy.
018 *
019 * @version
020 * 2003-07-24
021 * @since
022 * 2002-03-08
023 * @author
024 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
025 *********************************************************************/
026
027 public class BufferStrategyAnimatedComponent
028 extends AnimatedComponent
029 //////////////////////////////////////////////////////////////////////
030 //////////////////////////////////////////////////////////////////////
031 {
032
033 protected final BufferStrategy bufferStrategy;
034
035 //
036
037 private final Rectangle componentBounds;
038
039 private final Rectangle clipBounds;
040
041 //////////////////////////////////////////////////////////////////////
042 // constructor methods
043 //////////////////////////////////////////////////////////////////////
044
045 /*********************************************************************
046 * Main constructor.
047 *********************************************************************/
048 public BufferStrategyAnimatedComponent (
049 ComponentAnimator componentAnimator,
050 RepaintCollector repaintCollector,
051 LoopGovernor loopGovernor,
052 BufferStrategy bufferStrategy )
053 //////////////////////////////////////////////////////////////////////
054 {
055 super ( componentAnimator, repaintCollector, loopGovernor );
056
057 NullArgumentException.check (
058 this.bufferStrategy = bufferStrategy );
059
060 componentBounds = new Rectangle ( );
061
062 clipBounds = new Rectangle ( );
063 }
064
065 public BufferStrategyAnimatedComponent (
066 ComponentAnimator componentAnimator,
067 BufferStrategy bufferStrategy )
068 //////////////////////////////////////////////////////////////////////
069 {
070 this (
071 componentAnimator,
072 DefaultAnimationFactory.INSTANCE.createRepaintCollector ( ),
073 new FixedDelayLoopGovernor ( 0, 0 ),
074 bufferStrategy );
075 }
076
077 //////////////////////////////////////////////////////////////////////
078 // protected methods
079 //////////////////////////////////////////////////////////////////////
080
081 protected void animate ( )
082 //////////////////////////////////////////////////////////////////////
083 {
084 componentAnimator.update ( this );
085
086 int count = repaintCollector.getCount ( );
087
088 Rectangle [ ] repaintRegions
089 = repaintCollector.getRepaintRegions ( );
090
091 getBounds ( componentBounds );
092
093 Graphics2D graphics2D
094 = ( Graphics2D ) bufferStrategy.getDrawGraphics ( );
095
096 for ( int i = 0; i < count; i++ )
097 {
098 Rectangle repaintRegion = repaintRegions [ i ];
099
100 if ( !componentBounds.intersects ( repaintRegion ) )
101 {
102 continue;
103 }
104
105 Rectangle2D.intersect (
106 componentBounds, repaintRegion, clipBounds );
107
108 graphics2D.setClip ( clipBounds );
109
110 componentAnimator.paint ( this, graphics2D );
111 }
112
113 bufferStrategy.show ( );
114
115 graphics2D.dispose ( );
116
117 repaintCollector.reset ( );
118 }
119
120 //////////////////////////////////////////////////////////////////////
121 //////////////////////////////////////////////////////////////////////
122 }