ok, here it is: a plane showing a sprite. It’s very simplistic, uncommented, without transparency and fancy features, but it works.
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.image.BufferedImage;
import java.util.Enumeration;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class Sprite extends BranchGroup
{
private ImageComponent2D[] images;
private Texture2D texture;
private int initialLoopCount;
private int loopCount;
private int imageIndex = 0;
private SpriteAnimation anim;
public Sprite(ImageComponent2D[] images, long totalTime, int loopCount)
{
this.images = images;
this.loopCount = loopCount;
initialLoopCount = loopCount;
// prepare the textured appearance
texture = new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGB, 64, 64);
texture.setCapability(Texture.ALLOW_IMAGE_WRITE);
texture.setImage(0, images[0]);
Appearance app = new Appearance();
app.setTexture(texture);
// build the plane which will show the images
QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
// set plane corner coordinates
Point3f p = new Point3f();
p.set(-1.0f, 1.0f, 0.0f);
plane.setCoordinate(0, p);
p.set(-1.0f, -1.0f, 0.0f);
plane.setCoordinate(1, p);
p.set( 1.0f, -1.0f, 0.0f);
plane.setCoordinate(2, p);
p.set( 1.0f, 1.0f, 0.0f);
plane.setCoordinate(3, p);
// set texture coordinates
TexCoord2f q = new TexCoord2f();
q.set(0.0f, 1.0f);
plane.setTextureCoordinate(0, 0, q);
q.set(0.0f, 0.0f);
plane.setTextureCoordinate(0, 1, q);
q.set(1.0f, 0.0f);
plane.setTextureCoordinate(0, 2, q);
q.set(1.0f, 1.0f);
plane.setTextureCoordinate(0, 3, q);
// add it
this.addChild(new Shape3D(plane, app));
// adds the image updater to animate the sprite
anim = new SpriteAnimation(totalTime / images.length);
anim.setSchedulingBounds(this.getBounds());
this.addChild(anim);
}
public void reset()
{
loopCount = initialLoopCount;
imageIndex = 0;
anim.setEnable(true);
}
public void stop()
{
anim.setEnable(false);
}
/**
* This behavior changes the texture image at given time intervals
*/
private class SpriteAnimation extends Behavior
{
private WakeupCriterion wakeupCondition;
SpriteAnimation (long delay)
{
wakeupCondition = new WakeupOnElapsedTime(delay);
}
public void initialize()
{
this.wakeupOn(wakeupCondition);
}
public void processStimulus(Enumeration criteria)
{
texture.setImage(0, images[imageIndex]);
imageIndex++;
if (imageIndex == images.length)
{
imageIndex = 0;
if (loopCount > 0)
loopCount--;
if (loopCount == 0)
this.setEnable(false);
}
this.wakeupOn(wakeupCondition);
}
}
public static void main(String[] args) throws Exception
{
SimpleUniverse u = new SimpleUniverse();
u.getViewingPlatform().setNominalViewingTransform();
TextureLoader loader;
ImageComponent2D[] images = new ImageComponent2D[12];
for (int i=1; i<=12; i++)
{
loader = new TextureLoader("explosion" + i + ".png", null);
images[i-1] = loader.getImage();
}
Sprite sprite = new Sprite(images, 1000, -1);
u.addBranchGraph(sprite);
}
}