问题描述:
Java Applet使用
参考资料:
http://docs.oracle.com/javase/tutorial/deployment/applet/deployingApplet.html
问题解决:
Java 代码 extends JApplet
LocCube.java文件:
package emos.position.view; import java.awt.BorderLayout; import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.vecmath.*; public class LocCube extends JApplet { private static final long serialVersionUID = 1L;
private Color color = new Color();
private JPanel panel = new JPanel(); /**
* Java3D 使用
* @return
*/
private BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(0d, 0d, 0d),
100d); Background bg = new Background(color.diffuse);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg); Transform3D ts = new Transform3D(); ts.rotX(-(double) Math.PI / 8); // 旋转X轴
Vector3f vector = new Vector3f(-5f, -5, -25f); // 指定点平移
ts.setTranslation(vector); TransformGroup tg = new TransformGroup(ts);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); /**
* 鼠标旋转
*/
MouseRotate mrot = new MouseRotate(tg);
mrot.setSchedulingBounds(new BoundingSphere());
tg.addChild(mrot); /**
* 鼠标滚轮放大
*/
MouseWheelZoom zoom = new MouseWheelZoom();
zoom.setTransformGroup(tg);
zoom.setSchedulingBounds(new BoundingSphere());
tg.addChild(zoom); /**
* 鼠标右键平移
*/
MouseTranslate translate = new MouseTranslate();
translate.setTransformGroup(tg);
translate.setSchedulingBounds(new BoundingSphere());
tg.addChild(translate); Shape3D linecube = new LineCube();
Shape3D linecross = new LineCross();
tg.addChild(linecube);
tg.addChild(linecross); objRoot.addChild(tg);
objRoot.compile();
return objRoot;
} public LocCube() {
panel.setLayout(new BorderLayout());
// setLayout(new BorderLayout());
Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
panel.add("Center", c);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
u.getViewer().getView().setMinimumFrameCycleTime(5);
} public JPanel getPanel() {
return panel;
} public void init() {
new LocCube();
add(getPanel());
} /*
* public static void main(String[] args) { JFrame frame = new JFrame();
* LocCube panel = new LocCube(); frame.add(panel.getPanel());
* frame.setSize(200, 200); frame.setVisible(true); }
*/
}
JSP文件包含applet程序:
applet.jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p align="center">
<applet code="emos.position.view.LocCube" archive="emos.jar" width="700"
height="500"> </applet>
</p>
<p align="center">java applet</p>
</body>
</html>
注:
以上jsp文件,包含applet小程序的路径配置尤其需要注意,其中code表示LocCube.java 文件生成LocCube.class 文件的位置
archive表示包含LocCube.class文件的jar包文件位置,生成的jar包文件和在同一目录
详细来说:
1、生成emos.jar 文件
Dynamic Java Web 项目的文件目录如上所示,其中class文件位于build/classes/emos/position/view/…,使用jar工具,生成jar包文件
生成emos.jar 文件:
将如上生成的emos.jar 文件放置在WebContent目录,放置于和applet.jsp同一目录之下。其中LocCube.class位于emos.jar 文件中的
emos/position/view/目录之下。
运行applet.jsp 文件:
注:
第一次在浏览器中使用applet小程序,需要安装Java插件,安装成功之后,配置Java插件支持浏览器类型:
如上图所示,包含了Java所在位置:控制面板——程序,以及设置java插件浏览器支持类型
运行结果如下: