|
/**
* Animate.java
*
* @author Martin Klöckner, Daniel Höpfl
*
* @see IAnimatedOutput
*/
package SpringerPack;
import SpringerPack.*;
import SpringerPack.Boards.*;
import java.awt.Polygon;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Canvas;
import java.awt.Font;
import java.applet.Applet;
import javax.swing.Timer;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.*;
import java.util.*;
/**
* this class implements functions to animate the result which where found
* by the Springer-Algorithm.
* each board which implements the abstract class IAnimatedOutput
* uses these functions .
*
* @see IAnimatedOutput
*/
public class Animate extends Canvas implements ActionListener
{
private Vector mResultVec = new Vector();
private int mCurrentPos = 0;
private String mImageName = "CISLogo.gif";
private IAnimatedOutput mOutput;
private Timer mTimer;
private Point mStartPoint = new Point( 0, 0 );
private Point mEndPoint = new Point( 0, 0 );
private boolean mAnimationStop = false;
private int mFrameNumber;
private int mSteps = 7;
private Image mFigure;
private int mWidth;
private int mHeight;
private Image mImage;
private boolean mAutostepping = false;
private boolean mDirectionIsDown = false;
/**
* constructor of the class
* it just calls the init function with its given parameters.
*
* @param inOutput OutputBoard (inherited from IAnimatedOutput)
*
* @param inResult result from the Result-List
*
* @param inImage image of a Springer-figure
*/
public Animate ( IAnimatedOutput inOutput, String inResult, Image inImage)
{
init( inOutput, inResult, inImage );
}
/**
* inits the class after creation
* @param inOutput OutputBoard (inherited from IAnimatedOutput)
*
* @param inResult result from the Result-List
*
* @param inImage image of a Springer-figure
*/
public void init( IAnimatedOutput inOutput, String inResult, Image inImage )
{
mFigure = inImage;
mOutput = inOutput;
setResult( inResult );
mTimer = new Timer( 3, this );
// first occurence of Timer-Event
mTimer.setInitialDelay( 5 );
/// actionPerformed-events are not queued, even if the app is busy
mTimer.setCoalesce( true );
mEndPoint=translateKoord( (String)(mResultVec.elementAt( mCurrentPos )) );
mStartPoint=mEndPoint;
mFrameNumber=mSteps;
mOutput.setSize( new java.awt.Dimension( mWidth, mHeight ) );
stepBackward();
repaint();
}
/**
* the amount of frames, the animation takes per step
*
* @param inSpeed number of frames per animation-step
*/
public void setAnimationSpeed( int inSpeed )
{
mSteps = inSpeed;
}
/**
* continues in with animation while not reached Start or Stop
*
* @param inStep true or false
*/
public void setAutoStep( boolean inStep )
{
mAutostepping = inStep;
}
/**
* sets an Image, to be able to draw off-screen
* reduces the flicker
*
* @param inImage Image to draw off-screen
*/
public void setImage( Image inImage )
{
if ( inImage != null )
{
mImage = inImage;
}
}
/**
* sets the result-string which is selected in the Result-List
*
* @param inResult result which will be animated
*/
public void setResult( String inResult )
{
StringTokenizer st = new StringTokenizer( inResult );
mCurrentPos = 0;
mResultVec.removeAllElements();
while( st.hasMoreTokens() )
{
mResultVec.addElement( st.nextToken() );
}
}
/**
* translate the coordinates (e.g. A1 -> 1,1)
* returns the result in a Point
*
* @param inKoor e.g A1
*/
private Point translateKoord( String inKoor )
{
Point p;
String fields = "ABCDEFGHIJKLMNOP";
int x = 0;
int y = 0;
x = new Integer( fields.indexOf( inKoor.substring(0,1) ) ).intValue()+1;
y = new Integer( inKoor.substring(1, inKoor.length() ) ).intValue();
p = new Point( x, y );
return p;
}
/**
* sets the Figure to the next field
*/
public boolean stepForward()
{
Point StartPoint;
Point StopPoint = new Point( 0, 0 );
mDirectionIsDown = true;
StartPoint = translateKoord( (String)(mResultVec.elementAt( mCurrentPos )) );
mCurrentPos++;
if ( mCurrentPos >= mResultVec.size() )
{
mCurrentPos--;
return false;
}
StopPoint = translateKoord( (String)(mResultVec.elementAt( mCurrentPos )) );
animateFigure( StartPoint, StopPoint );
return true;
}
/**
* sets the Figure one step back
*/
public boolean stepBackward()
{
Point StartPoint;
Point StopPoint;
mDirectionIsDown = false;
StartPoint = translateKoord( (String)(mResultVec.elementAt( mCurrentPos )) );
mCurrentPos--;
if ( mCurrentPos < 0)
{
mCurrentPos++;
return false;
}
StopPoint = translateKoord( (String)(mResultVec.elementAt( mCurrentPos )) );
animateFigure( StartPoint, StopPoint );
return true;
}
/**
*
* finds the coordinaten of the next field
*/
public boolean nextStep( Point inPStart, Point inPStop )
{
if ( ( inPStart.x != inPStop.x ) || (inPStart.y != inPStop.y ) )
{
if( inPStart.x < inPStop.x )
{
inPStart.setLocation( inPStart.x+1, inPStart.y );
return true;
}
else if( inPStart.x > inPStop.x )
{
inPStart.setLocation( inPStart.x-1, inPStart.y );
return true;
}
else if( inPStart.x == inPStop.x )
{
if( inPStart.y < inPStop.y )
{
inPStart.setLocation(inPStart.x, inPStart.y +1);
return true;
}
else if ( inPStart.y > inPStop.y )
{
inPStart.setLocation( inPStart.x, inPStart.y-1);
return true;
}
}
return false;
}
else
{
return false;
}
}
/**
* starts the animation of the figure
*
* @param inXStart X-coordinate of the starting field
*
* @param inYStart Y-coordinate of the starting field
*
* @param inXStop X-coordinate of the stopping field
*
* @param inYStop Y-coordinate of the stopping field
*/
protected void animateFigure( int inXStart, int inYStart, int inXStop, int inYStop )
{
Point PStart = new Point ( inXStart, inYStart );
Point PStop = new Point ( inXStop, inYStop );
animateFigure( PStart, PStop );
}
/**
* starts the animation of the figure
*
* @param inPStart startingpoint of the animation
*
* @param inPStop stoppoint of the animation
*/
public void animateFigure( Point inPStart, Point inPStop)
{
mStartPoint = inPStart;
mEndPoint = inPStop;
// this is for Animation or not!
mTimer.start();
mFrameNumber=mSteps;
repaint();
}
/**
* function is called by the TimerEvent
*/
public void actionPerformed( ActionEvent e )
{
mFrameNumber--;
// bail out or not?
if ( mFrameNumber <= 0 )
mAnimationStop = true;
else
mAnimationStop = false;
repaint();
if ( mAnimationStop )
{
mTimer.stop();
mAnimationStop=false;
if ( mAutostepping == true )
{
if ( mDirectionIsDown == true )
{
stepForward();
}
else
{
stepBackward();
}
}
}
}
/**
* sets the size of the Canvas
*/
public void setSize(java.awt.Dimension inDim)
{
super.setSize(inDim);
mWidth = inDim.width - 1;
mHeight = inDim.height - 1;
//mFontSize = -1;
repaint();
}
public void update(Graphics inGraphics) {
// super.update would clear the drawing area
// since we should not have any childs we leave it out
super.update(inGraphics);
// repaint the board (using offscreen - or not)
paint(inGraphics);
}
public void paint( Graphics inG )
{
Point sP = mOutput.getFieldMiddle( mStartPoint );
Point eP = mOutput.getFieldMiddle( mEndPoint );
Point p = new Point( 1,1 );
Graphics g = inG;
// if there is an Image -> draw offscreen
// we will copy it to the screen later
if(mImage != null)
{
g = mImage.getGraphics();
}
// Insert the painting of the board here!!!
double deltaX = ( eP.x - sP.x )/mSteps;
double deltaY = ( eP.y - sP.y )/mSteps;
int xPos = -(int)((mFrameNumber)*deltaX);
int yPos = -(int)((mFrameNumber)*deltaY);
xPos -= (int)(mFigure.getWidth( this )/2);
yPos -= (int)(mFigure.getHeight( this )/2);
mOutput.setSize( new java.awt.Dimension( mWidth, mHeight ) );
mOutput.paint( g );
g.drawImage( mFigure, (int)(eP.x)+xPos, (int)(eP.y)+yPos, this);
if ( mImage != null )
inG.drawImage( mImage, 0, 0, this );
}
}
|