JAVA 3D 实时 旋转

下面我们给出实现JAVA 3D 实时旋转的代码,根据SOCKET接受到的数据进行旋转,SOCKET接受到的数据是三个旋转后的角度,然后3D图形根据这三个角度来实时的更新,如果有不懂的可以问我。

 

//3d 显示图像的代码

package com.neuera.service;

 

import com.sun.j3d.utils.applet.MainFrame;

import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.universe.*;

import java.applet.Applet;

import java.awt.BorderLayout;

import java.awt.Frame;

import java.awt.GraphicsConfiguration;

import java.io.IOException;

 

import javax.media.j3d.*;

import javax.vecmath.*;

 

/**

 *  根据事件来旋转立方体

 *  事件是每秒执行一次

 */

public class Java3DExample extends Applet{

 

public  BranchGroup scene;//场景

public RotationInterpolator rotatorX;//旋转X

public RotationInterpolator rotatorY;//旋转Y

public RotationInterpolator rotatorZ;//旋转Z

public static Frame frame;//窗口

public BranchGroup objRoot;//旋转根节点

public SimpleUniverse simpleU;//显示

public Alpha rotationAlpha;

/**

* 构造函数

*/

public Java3DExample(){

this.init();

}

/**

* 初始化

*/

    public void init() {

    setLayout(new BorderLayout());

GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

Canvas3D canvas3D = new Canvas3D(config);

add("Center", canvas3D);

try {

scene = createSceneGraph();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// SimpleUniverse is a Convenience Utility class

simpleU = new SimpleUniverse(canvas3D);

simpleU.getViewingPlatform().setNominalViewingTransform();

simpleU.addBranchGraph(scene);

    }

 

    public BranchGroup createSceneGraph() throws InterruptedException {

// Create the root of the branch graph

objRoot = new BranchGroup();

objRoot.setCapability(BranchGroup.ALLOW_DETACH);

TransformGroup objRotate = new TransformGroup();

objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

objRoot.addChild(objRotate);

ColorCube cube=new ColorCube(0.4);

objRotate.addChild(cube);

objRoot.addChild(new Axis());

/*****行为定义******/

TimeBehavior b=new TimeBehavior();

b.setObjRoot(objRoot);

        BoundingSphere movingBounds = new BoundingSphere(new Point3d(0.0, 0.0,0.0), 100.0);

        b.setSchedulingBounds(movingBounds);

        objRoot.addChild(b);

 

objRoot.compile();

return objRoot;

}

 

    /**

     * 主函数,显示窗口

     * @param args

     * @throws Exception 

     * @throws IOException 

     */

    public static void main(String[] args) throws IOException, Exception {

    Java3DExample.frame = new MainFrame(new Java3DExample(),512,512);

    new SocketServer().service();

    }

}

 

 

// Behavior

 

 

package com.neuera.service;

 

import java.util.Enumeration;

import javax.media.j3d.Behavior;

import javax.media.j3d.BranchGroup;

import javax.media.j3d.Transform3D;

import javax.media.j3d.TransformGroup;

import javax.media.j3d.WakeupOnElapsedTime;

import javax.vecmath.Matrix3d;

 

public class TimeBehavior extends Behavior {

 

public BranchGroup objRoot;

 

public BranchGroup getObjRoot() {

return objRoot;

}

 

public void setObjRoot(BranchGroup objRoot) {

this.objRoot = objRoot;

}

 

public static int temp = 0;

 

public static double angleX;// X轴方向的角度

public static double angleY;// Y轴方向的角度

public static double angleZ;// Z轴方向的角度

 

/**

* 初始化

*/

@Override

public void initialize() {

this.wakeupOn(new WakeupOnElapsedTime(100));// 添加事件,每秒一次

}

 

@Override

public void processStimulus(Enumeration arg0) {

this.wakeupOn(new WakeupOnElapsedTime(500));

 

System.out.println("收到数据");

Matrix3d m1 = new Matrix3d();//x 方向

Matrix3d m2 = new Matrix3d();//y 方向

Matrix3d m3 = new Matrix3d();//z 方向

m1.rotX(Math.toRadians(angleX));// 绕X轴旋转45度。

m2.rotY(Math.toRadians(angleY));// 绕y轴旋转45度。

m3.rotZ(Math.toRadians(angleZ));// 绕z轴旋转45度。

//

m2.mul(m3);

m1.mul(m2);

Transform3D transform = new Transform3D();

transform.setRotation(m1);

 

TransformGroup a = (TransformGroup) objRoot.getChild(0);

// objRotate.setTransform(axis);

a.setTransform(transform);

}

 

}



//socket 接受数据


package com.neuera.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
public class SocketServer {
private final int port = 8888;// 服务器端的端口号
private ServerSocket serverSocket;
private ExecutorService executorService;// 线程池
private final int POOL_SIZE = 10;// 单个CPU线程池大小

public SocketServer() throws IOException {
serverSocket = new ServerSocket(port);
// Runtime的availableProcessor()方法返回当前系统的CPU数目.
executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * POOL_SIZE);
System.out.println("服务器已经启动。。。。。");
}

public void service() throws Exception {
while (true) {
Socket socket = null;
// Socket socket1=new Socket("192.168.0.200",162);
try {
// 接收客户连接,只要客户进行了连接,就会触发accept();从而建立

连接
socket = serverSocket.accept();
executorService.execute(new Handler(socket));

} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
new SocketServer().service();
}
}

class Handler implements Runnable{
private Socket socket;
private BufferedReader br; // 得到客户端的输入来的数据
private InputStream dataIn;
private PrintWriter pw; // 可以传送给客户端的Writer
public Handler(Socket socket) throws IOException {
this.socket = socket;
this.br = getReader(socket);
this.dataIn=socket.getInputStream();
this.pw = getWriter(socket);
}

private PrintWriter getWriter(Socket socket) throws IOException {
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}

private BufferedReader getReader(Socket socket) throws IOException {
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}

//接受客户端的链接
public void run() {
try {
System.out.println("New connection accepted "+ 

socket.getInetAddress() + ":" + socket.getPort());
String msg = null;
while ((msg = br.readLine()) != null) {
//发来的数据
System.out.println(msg);
String angles[]=msg.split(",");
TimeBehavior.angleX=-Double.parseDouble(angles[0]);
TimeBehavior.angleY=Double.parseDouble(angles[2]);
TimeBehavior.angleZ=-Double.parseDouble(angles[1]);
System.out.println(TimeBehavior.angleX);
System.out.println(TimeBehavior.angleY);
System.out.println(TimeBehavior.angleZ);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("结束了");
}
}
}

//坐标轴

package com.neuera.service;

import javax.media.j3d.Geometry;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.IndexedLineArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3f;

public class Axis extends Shape3D {

// //
//
// create axis visual object
//
public Axis() {

this.setGeometry(createGeometry());

}

private Geometry createGeometry() {
// create line for X axis
IndexedLineArray axisLines = new IndexedLineArray(18,
GeometryArray.COORDINATES, 30);

axisLines.setCoordinate(0, new Point3f(-1.0f, 0.0f, 0.0f));
axisLines.setCoordinate(1, new Point3f(1.0f, 0.0f, 0.0f));
axisLines.setCoordinate(2, new Point3f(0.9f, 0.1f, 0.1f));
axisLines.setCoordinate(3, new Point3f(0.9f, -0.1f, 0.1f));
axisLines.setCoordinate(4, new Point3f(0.9f, 0.1f, -0.1f));
axisLines.setCoordinate(5, new Point3f(0.9f, -0.1f, -0.1f));
axisLines.setCoordinate(6, new Point3f(0.0f, -1.0f, 0.0f));
axisLines.setCoordinate(7, new Point3f(0.0f, 1.0f, 0.0f));
axisLines.setCoordinate(8, new Point3f(0.1f, 0.9f, 0.1f));
axisLines.setCoordinate(9, new Point3f(-0.1f, 0.9f, 0.1f));
axisLines.setCoordinate(10, new Point3f(0.1f, 0.9f, -0.1f));
axisLines.setCoordinate(11, new Point3f(-0.1f, 0.9f, -0.1f));
axisLines.setCoordinate(12, new Point3f(0.0f, 0.0f, -1.0f));
axisLines.setCoordinate(13, new Point3f(0.0f, 0.0f, 1.0f));
axisLines.setCoordinate(14, new Point3f(0.1f, 0.1f, 0.9f));
axisLines.setCoordinate(15, new Point3f(-0.1f, 0.1f, 0.9f));
axisLines.setCoordinate(16, new Point3f(0.1f, -0.1f, 0.9f));
axisLines.setCoordinate(17, new Point3f(-0.1f, -0.1f, 0.9f));

axisLines.setCoordinateIndex(0, 0);
axisLines.setCoordinateIndex(1, 1);
axisLines.setCoordinateIndex(2, 2);
axisLines.setCoordinateIndex(3, 1);
axisLines.setCoordinateIndex(4, 3);
axisLines.setCoordinateIndex(5, 1);
axisLines.setCoordinateIndex(6, 4);
axisLines.setCoordinateIndex(7, 1);
axisLines.setCoordinateIndex(8, 5);
axisLines.setCoordinateIndex(9, 1);
axisLines.setCoordinateIndex(10, 6);
axisLines.setCoordinateIndex(11, 7);
axisLines.setCoordinateIndex(12, 8);
axisLines.setCoordinateIndex(13, 7);
axisLines.setCoordinateIndex(14, 9);
axisLines.setCoordinateIndex(15, 7);
axisLines.setCoordinateIndex(16, 10);
axisLines.setCoordinateIndex(17, 7);
axisLines.setCoordinateIndex(18, 11);
axisLines.setCoordinateIndex(19, 7);
axisLines.setCoordinateIndex(20, 12);
axisLines.setCoordinateIndex(21, 13);
axisLines.setCoordinateIndex(22, 14);
axisLines.setCoordinateIndex(23, 13);
axisLines.setCoordinateIndex(24, 15);
axisLines.setCoordinateIndex(25, 13);
axisLines.setCoordinateIndex(26, 16);
axisLines.setCoordinateIndex(27, 13);
axisLines.setCoordinateIndex(28, 17);
axisLines.setCoordinateIndex(29, 13);

return axisLines;

} // end of Axis createGeometry()

} // end of class Axis

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值