ホームに戻る
 アニメ

図04_00:三角形が左回転するアニメ

/*
*   JAVA3D でアニメアプレット
*/

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;

/* <applet code="java3Dtest04" width=300 height=300></applet> */

public class java3Dtest04 extends Applet implements Runnable{
  private SimpleUniverse universe = null;
  private Canvas3D canvas3d = null;
  private boolean mLoop = false;
  private Thread mThread = null;
  private boolean mInited = false;
  private long mCycle = 6000;
  private long mTick = 0;
  private long mTimeOld;
  private float mFInterval = 1.0f / 2000.0f;

  public void init(){
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
    canvas3d = new Canvas3D(config);
    this.setLayout(new BorderLayout());
    this.add(canvas3d, BorderLayout.CENTER);

    canvas3d.stopRenderer();
    canvas3d.setDoubleBufferEnable(true);

    universe = new SimpleUniverse(canvas3d);

    universe.getViewingPlatform().setNominalViewingTransform();

    mTimeOld = System.currentTimeMillis();
    mInited = true;
  }

  public void render(){
    GraphicsContext3D gc;
    gc = canvas3d.getGraphicsContext3D();
    gc.clear();

    Point3d[] vertices = new Point3d[3];

    vertices[0] = new Point3d(0.0, 0.5, 0.0);
    vertices[1] = new Point3d(-0.433, -0.25, 0.0);
    vertices[2] = new Point3d(0.433, -0.25, 0.0);

    TriangleArray geometry = new TriangleArray(vertices.length, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
    geometry.setCoordinates(0, vertices);
    Color3f red = new Color3f(1.0f, 0.0f, 0.0f); 
    Color3f green = new Color3f(0.0f, 1.0f, 0.0f); 
    Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
    Color3f[] colors = {red, green, blue};
    geometry.setColors(0, colors);

    Shape3D shape = new Shape3D(geometry);

    Transform3D trans = new Transform3D();

    double angle;
    angle = (float)mTick * mFInterval;
    angle *= Math.PI * 2.0;

    trans.set(Transform3D.ZERO);
    trans.rotZ(angle);

    gc.setModelTransform(trans);
    gc.draw(shape);

    canvas3d.swap();
  }

  public void start(){
    mLoop = true;

    if(mThread == null){
      mThread = new Thread(this);
      mThread.start();
    }
    else{
      if(!mThread.isAlive()){
        mThread = new Thread(this);
        mThread.start();
      }
    }
  }

  public void run(){
    long current,elapsed;
    while(mLoop){
      current = System.currentTimeMillis();
      elapsed = current - mTimeOld;
      if(elapsed > 15){
        mTimeOld = current;
        mTick += elapsed;
        mTick %= mCycle;
        if(mInited){
          render();
        }
      }
      mThread.yield();
    }
  }

  public void stop(){ 
    mLoop = false;
  }

  public void destroy(){
    universe.cleanup();
  }
}

inserted by FC2 system