leftrightupdater.java
来自「CroftSoft Code Library是一个开源的可移植的纯Java游戏库」· Java 代码 · 共 104 行
JAVA
104 行
package com.croftsoft.apps.sprite;
import java.awt.Rectangle;
import javax.swing.Icon;
import javax.swing.JComponent;
import com.croftsoft.core.lang.NullArgumentException;
import com.croftsoft.core.animation.ComponentUpdater;
import com.croftsoft.core.animation.sprite.IconSprite;
/*********************************************************************
* Switches the Icon based upon the Sprite heading.
*
* @version
* 2002-03-23
* @since
* 2002-02-24
* @author
* <a href="http://www.croftsoft.com/">David Wallace Croft</a>
*********************************************************************/
public final class LeftRightUpdater
implements ComponentUpdater
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
{
private final IconSprite iconSprite;
private final Icon lookLeftIcon;
private final Icon lookRightIcon;
private final Rectangle repaintRegion;
//
private double oldHeading;
private Icon oldIcon;
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public LeftRightUpdater (
IconSprite iconSprite,
Icon lookLeftIcon,
Icon lookRightIcon )
//////////////////////////////////////////////////////////////////////
{
NullArgumentException.check ( this.iconSprite = iconSprite );
NullArgumentException.check ( this.lookLeftIcon = lookLeftIcon );
NullArgumentException.check ( this.lookRightIcon = lookRightIcon );
repaintRegion = new Rectangle ( );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public void update ( JComponent component )
//////////////////////////////////////////////////////////////////////
{
double heading = iconSprite.getHeading ( );
if ( heading == oldHeading )
{
return;
}
oldHeading = heading;
Icon icon = lookRightIcon;
if ( ( heading > Math.PI / 2 )
&& ( heading < 3 * Math.PI / 2 ) )
{
icon = lookLeftIcon;
}
if ( icon == oldIcon )
{
return;
}
iconSprite.getPaintBounds ( repaintRegion );
component.repaint ( repaintRegion );
iconSprite.setIcon ( icon );
oldIcon = icon;
iconSprite.getPaintBounds ( repaintRegion );
component.repaint ( repaintRegion );
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?