[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Java3Djp:00877] Pickingの別解(2/2)
☆★☆★☆★☆ホームページスペースのことならAKB NETWORKへ★☆★☆★☆★
★☆ http://あなたのID.miffy.toでHPを作るなら→\890/月から! ★☆
☆ 50Mの大容量!!!独自ドメインでのホスティングなら→\3,980/月から! ★
★ サーバー丸ごとレンタル。Webレンタル業務も簡単→\20,000/月から! ☆
☆★ ただいまキャンペーン中!更にお安く提供しています!! ☆★
★☆★☆★☆★業界屈指の低料金!!http://www.akb-net.com/☆★☆★☆★☆
えんどう@続き です。
> 利用例は後ほど...
// Java 3D Test Applet
// PickMouseTest.java
// Copyright (c) 1999 ENDO Yasuyuki
// mailto:yasuyuki@xxxxxxxxxx
// http://www.javaopen.org/j3dbook/index.html
import java.applet.*;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.picking.*;
public class PickMouseTest extends Applet {
private Canvas3D canvas_ = null;
private SimpleUniverse universe_ = null;
private BranchGroup scene_ = null;
private TransformGroup objTrans_ = null;
private Texture texture_ = null;
private boolean isStandalone = false;
public PickMouseTest() {
this(false);
}
public PickMouseTest(boolean isStandalone) {
this.isStandalone = isStandalone;
}
public void init() {
canvas_ = new Canvas3D(null);
this.setLayout(new BorderLayout());
this.add(canvas_, "Center");
universe_ = new SimpleUniverse(canvas_);
universe_.getViewingPlatform().setNominalViewingTransform();
scene_ = createSceneGraph();
universe_.addBranchGraph(scene_);
}
private BranchGroup createSceneGraph() {
BranchGroup root = new BranchGroup();
BoundingSphere bounds = new BoundingSphere( new Point3d(), 100.0 );
PickRotateBehavior rotator =
new PickRotateBehavior(root, canvas_, bounds, PickObject.USE_GEOMETRY);
root.addChild(rotator);
PickTranslateBehavior translator =
new PickTranslateBehavior(root, canvas_, bounds, PickObject.USE_GEOMETRY);
root.addChild(translator);
PickZoomBehavior zoomer =
new PickZoomBehavior(root, canvas_, bounds, PickObject.USE_GEOMETRY);
root.addChild(zoomer);
SimplePicking picker =
new SimplePicking( root, canvas_, bounds,
PickObject.USE_GEOMETRY, PickObject.PRIMITIVE );
picker.setupCallback( new SimplePickingCallback() {
public void picked(int type, Node node) {
if (node != null) {
String data = (String)node.getUserData();
System.out.println(data);
} else {
System.out.println("Error: node is null.");
}
}
});
root.addChild(picker);
Transform3D t3d = new Transform3D();
objTrans_ = new TransformGroup(t3d);
TransformGroup gtrans = new TransformGroup();
gtrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
gtrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
gtrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
objTrans_.addChild(gtrans);
double[] vertices = { -0.8, -0.8, 0.0,
-0.8, 0.8, 0.0,
-0.4, -0.8, 0.0,
-0.4, 0.8, 0.0,
0.0, -0.8, 0.0,
0.0, 0.8, 0.0
0.4, -0.8, 0.0,
0.4, 0.8, 0.0,
0.8, -0.8, 0.0,
0.8, 0.8, 0.0,
-0.8, -0.8, 0.0,
0.8, -0.8, 0.0,
-0.8, -0.4, 0.0,
0.8, -0.4, 0.0,
-0.8, 0.0, 0.0,
0.8, 0.0, 0.0,
-0.8, 0.4, 0.0,
0.8, 0.4, 0.0,
-0.8, 0.8, 0.0,
0.8, 0.8, 0.0 };
LineArray geom = new LineArray( vertices.length, GeometryArray.COORDINATES);
geom.setCoordinates(0, vertices);
geom.setCapability(Geometry.ALLOW_INTERSECT);
Shape3D grid = new Shape3D(geom);
grid.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
gtrans.addChild(grid);
Appearance ap = createAppearance();
Transform3D bt3d = new Transform3D();
bt3d.set(new Vector3d(-0.4, 0.4, 0.0));
TransformGroup btrans = new TransformGroup(bt3d);
btrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
btrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
btrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Box box =
new Box( 0.25f, 0.25f, 0.25f,
Primitive.GENERATE_TEXTURE_COORDS,
ap );
box.setUserData("this is box.");
box.getShape(Box.TOP).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
box.getShape(Box.BOTTOM).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
box.getShape(Box.BACK).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
box.getShape(Box.FRONT).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
box.getShape(Box.LEFT).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
box.getShape(Box.RIGHT).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
box.getShape(Box.TOP).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
box.getShape(Box.BOTTOM).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
box.getShape(Box.BACK).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
box.getShape(Box.FRONT).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
box.getShape(Box.LEFT).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
box.getShape(Box.RIGHT).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
btrans.addChild(box);
objTrans_.addChild(btrans);
Transform3D cyt3d = new Transform3D();
cyt3d.set(new Vector3d(-0.4, -0.4, 0.0));
TransformGroup cytrans = new TransformGroup(cyt3d);
cytrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
cytrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
cytrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Cylinder cylinder =
new Cylinder( 0.3f, 0.6f, Primitive.GENERATE_TEXTURE_COORDS, ap);
cylinder.setUserData("this is cylinder.");
cylinder.getShape(Cylinder.BODY).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
cylinder.getShape(Cylinder.TOP).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
cylinder.getShape(Cylinder.BOTTOM).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
cylinder.getShape(Cylinder.BODY).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
cylinder.getShape(Cylinder.TOP).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
cylinder.getShape(Cylinder.BOTTOM).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
cytrans.addChild(cylinder);
objTrans_.addChild(cytrans);
Transform3D cnt3d = new Transform3D();
cnt3d.set(new Vector3d(0.4, 0.4, 0.0));
TransformGroup cntrans = new TransformGroup(cnt3d);
cntrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
cntrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
cntrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Cone cone = new Cone( 0.3f, 0.6f, Primitive.GENERATE_TEXTURE_COORDS, ap);
cone.setUserData("this is cone.");
cone.getShape(Cone.BODY).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
cone.getShape(Cone.CAP).setCapability(Shape3D.ALLOW_GEOMETRY_READ);
cone.getShape(Cone.BODY).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
cone.getShape(Cone.CAP).getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
cntrans.addChild(cone);
objTrans_.addChild(cntrans);
Transform3D st3d = new Transform3D();
st3d.set(new Vector3d(0.4, -0.4, 0.0));
TransformGroup strans = new TransformGroup(st3d);
strans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
strans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
strans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
Sphere sphere = new Sphere( 0.3f, Primitive.GENERATE_TEXTURE_COORDS, ap );
sphere.setUserData("this is sphere.");
sphere.getShape().setCapability(Shape3D.ALLOW_GEOMETRY_READ);
sphere.getShape().getGeometry().setCapability(Geometry.ALLOW_INTERSECT);
strans.addChild(sphere);
objTrans_.addChild(strans);
root.addChild(objTrans_);
return root;
}
private Appearance createAppearance() {
Appearance app = new Appearance();
// Texture の生成
if (this.isStandalone) {
// アプリケーションとして実行されている
texture_ = new TextureLoader("earth.jpg", this).getTexture();
} else {
// アプレットとして実行されている
Image image = getImage(getCodeBase(), "earth.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
mt.checkAll(true);
try { mt.waitForID(0); } catch (InterruptedException e) { e.printStackTrace(); }
texture_ = new TextureLoader(image, this).getTexture();
}
app.setTexture(texture_);
return app;
}
public static void main(String[] args) {
PickMouseTest applet = new PickMouseTest(true); // isStandalone = true;
Frame frame = new MainFrame(applet, 500, 500);
}
}
---
ENDO Yasuyuki <yasuyuki@xxxxxxxxxx>
http://www.javaopen.org/jfriends/index.html (Japanese Only)