这篇文章被分为两个部分,描写我怎样实现一个Java 3D动画屏幕效果.在这个部分,我将说明我怎样利用JMF(Java Media Framework),非凡是在JMF Performance Pack for Windows v.2.1.1e情况下.我的另外两个工具是J2SE 5.0和Java 3D 1.3.2.我将讨论另外的使用Quicktime for Java的动画屏幕版本.
。程序执行使用Model-View-Controller设计模式.屏幕是一个视频元素,由JMFMovieScreen类描述.动画是一个由JMFSnapper类控制的模型部分.一个Java 3D Behavior类,TimeBehavior,控制动画定时定期更新.所有JMF编码都存放在JMFSnapper类,可以很方便的测试各种变化.这篇文章的第二部分JMFSnapper由QuickTime for Java版本中的QTSnapper取代.
// globalsprivate BranchGroup sceneBG;private JMFMovieScreen ms; // the movie screenprivate TimeBehavior timer; // to update screenprivate void addMovieScreen(String fnm){ // put the movie in fnm onto a movie screen ms = new JMFMovieScreen( new Point3f(1.5f, 0, -1), 2.0f, fnm); sceneBG.addChild(ms); // set up the timer for animating the movie timer = new TimeBehavior(40, ms); // update movie every 40ms (== 25 frames/sec) timer.setSchedulingBounds(bounds); sceneBG.addChild(timer);}
两个Java 3D addChild()方法调用JMFMovieScreen和TimeBehavior节点间的连接.setSchedulingBounds()激活TimeBehavior节点.
// globalsprivate Texture2D texture; // used by the quadprivate ImageComponent2D ic; private JMFSnapper snapper; // to take snaps of the movieprivate boolean isStopped = false; // is the movie stopped?public void nextFrame(){ if (isStopped) // movie has been stopped return; BufferedImage im = snapper.getFrame(); // get current frame if (im != null) {