package graphics.java;
import factories.Boxes;
import graphics.common.Box;
import graphics.common.Size;
import graphics.common.Point;
import graphics.common.GraphicsObject;
import graphics.common.GraphicsDB;
import utils.Receivable;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class JavaSprite implements GraphicsObject {
//private Object mImLock = new Object();
private BufferedImage mImage;
private Point mPoint;
private Size mSize;
private String name;
public JavaSprite( String name ) {
mImage = GraphicsDB.retrieveGraphic( name, BufferedImage.class );
if ( mImage == null ) {
Receivable toRegister = new Receivable() {
@Override
public void give( Object obj ) {
if ( !(obj instanceof BufferedImage) ) {
throw new IllegalArgumentException( "SoftwareSprite - Receivable.give: Not a BufferedImage." );
}
mImage = (BufferedImage) obj;
}
};
GraphicsDB.registerReceivable( toRegister, name );
mImage = TemporaryGraphic.getInstance();
}
}
public JavaSprite( String name, Point p ) {
this( name );
mPoint = p;
}
public JavaSprite( String name, Point p, Size s ) {
this( name );
mPoint = p;
mSize = s;
}
public void setLoc( double x, double y, double z ) {
mPoint.set( x, y, z );
}
public void setLoc( Point p ) {
mPoint.set( p );
}
public void modLoc( double x, double y, double z ) {
mPoint.modify( x, y, z );
}
public void modLoc( Point p ) {
mPoint.modify( p );
}
public Graphics2D getGraphics() {
return mImage.createGraphics();
}
@Override
public void draw( Graphics2D g, double delta ) {
g.drawImage( mImage, mPoint.getX(), mPoint.getY(),
mPoint.getX() + mSize.getWidth(), mPoint.getY() + mSize.getHeight(),
0, 0, mImage.getWidth(), mImage.getHeight(), null );
}
@Override
public String getName() {
return name;
}
@Override
public Point[] getPoints() {
return new Point[]{ mPoint };
}
@Override
public Size[] getSize() {
return new Size[]{ mSize };
}
@Override
public Box[] getBox() {
return new Box[] { Boxes.get( mPoint, mSize ) };
}
@Override
public void dispose() {
}
/*
@Override
public boolean equals( Object other ) {
if( !( other instanceof JavaSprite ) )
return false;
if( other == this )
return true;
JavaSprite jsOther = (JavaSprite)other;
if( jsOther.mPoint.getX() == mPoint.getX() &&
jsOther.mPoint.getY() == mPoint.getY() &&
jsOther.mSize.getWidth() == mSize.getWidth() &&
jsOther.mSize.getHeight() == mSize.getHeight() )
return true;
return false;
}
*/
}