[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Java3Djp:00211] Pickingのサンプル



■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■     ☆お店にない物を売るお店☆                 ■
■      平均50%のキャッシュバック!!        ■
■                比較見積りサイト!              ■
■    http://digitalforest.co.jp                      ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

Picking のサンプルです。あまり良いソースではないかもしれませんが、
コンパイルして楽しんでください。

それぞれの説明はまたの機会に...^^;

PickSelectionFeedback.java
=================================================================
// PickSelectionFeedback.java
// copyright (c) 1999 yasuyuki@xxxxxxxxxx

import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.behaviors.picking.PickObject;
import com.sun.j3d.utils.behaviors.picking.PickRotateBehavior;
import com.sun.j3d.utils.behaviors.picking.PickTranslateBehavior;
import com.sun.j3d.utils.behaviors.picking.PickZoomBehavior;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;

public class PickSelectionFeedback extends java.applet.Applet {

  private Canvas3D canvas3d = null;
  private BranchGroup objRoot = null;
  
  public void init(){
  }
  public void paint( Graphics g ){

  }
  public void update( Graphics g ){
  }
  public void start(){
  }
  public void stop(){
  }

  private Appearance createAppearance(float r, float g, float b) {
    Material material = new Material();
    material.setLightingEnable(true);
    material.setDiffuseColor(new Color3f(r, g, b));
    material.setSpecularColor(new Color3f(0.0f, 0.0f, 0.0f));
    material.setShininess(32.0f);
    Appearance appearance =  new Appearance();
    appearance.setMaterial(material);
    return appearance;
  }

  public BranchGroup createSceneGraph() {
    objRoot = new BranchGroup();

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

    DirectionalLight light =
      new DirectionalLight( new Color3f(1.0f, 1.0f, 1.0f),
                            new Vector3f(0.0f, 0.0f, -1.0f));
    light.setInfluencingBounds(bounds);
    
    objRoot.addChild(light);

    Cone cone =
      new Cone( 0.1f,
                0.2f,
                Primitive.GENERATE_NORMALS |
                Primitive.ENABLE_GEOMETRY_PICKING,
                createAppearance(1.0f, 0.0f, 0.0f));
    cone.setUserData("This is cone.");

    TransformGroup coneTrans = new TransformGroup();
    coneTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    coneTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    coneTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    Transform3D coneT3D = new Transform3D();
    coneT3D.setTranslation(new Vector3d(0.0, 0.5, 0.0));
    coneTrans.setTransform(coneT3D);

    coneTrans.addChild(cone);
    
    objRoot.addChild(coneTrans);
    
    Cylinder cylinder =
      new Cylinder( 0.1f,
                    0.2f,
                    Primitive.GENERATE_NORMALS |
                    Primitive.ENABLE_GEOMETRY_PICKING,
                    createAppearance(0.0f, 1.0f, 0.0f));
    cylinder.setUserData("This is cylinder.");
    
    TransformGroup cylinderTrans = new TransformGroup();
    cylinderTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    cylinderTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    cylinderTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    
    cylinderTrans.addChild(cylinder);
    
    objRoot.addChild(cylinderTrans);
    
    Box box =
      new Box( 0.1f,
               0.1f,
               0.1f,
               Primitive.GENERATE_NORMALS |
               Primitive.ENABLE_GEOMETRY_PICKING,
               createAppearance(0.0f, 0.0f, 1.0f));
    box.setUserData("This is box.");
    
    TransformGroup boxTrans = new TransformGroup();
    boxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    boxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    boxTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    Transform3D boxT3D = new Transform3D();
    boxT3D.setTranslation(new Vector3d(0.0, -0.5, 0.0));
    boxTrans.setTransform(boxT3D);
    
    boxTrans.addChild(box);
    
    objRoot.addChild(boxTrans);
    
    PickRotateBehavior rotate =
      new PickRotateBehavior(objRoot, canvas3d, bounds, PickObject.USE_GEOMETRY);
    objRoot.addChild(rotate);
    
    PickTranslateBehavior translate =
      new PickTranslateBehavior(objRoot, canvas3d, bounds, PickObject.USE_GEOMETRY);
    objRoot.addChild(translate);
    
    PickZoomBehavior zoom =
      new PickZoomBehavior(objRoot, canvas3d, bounds, PickObject.USE_GEOMETRY);
    objRoot.addChild(zoom);

    return objRoot;
  }

  public Node pickNode(int mx, int my) {
    PickObject  pickObject = new PickObject(canvas3d, objRoot);;
    SceneGraphPath sgPath = pickObject.pickClosest(mx, my, PickObject.USE_GEOMETRY);
    if (sgPath == null)
        return null;
    Node node = pickObject.pickNode(sgPath, PickObject.PRIMITIVE, 1);
    return node;
  }

  private void initialize3D() {
    setLayout(new BorderLayout());

    GraphicsConfiguration config =
      SimpleUniverse.getPreferredConfiguration();

    canvas3d = new Canvas3D(config);
    add("Center", canvas3d);
    
    //これでマウスイベントが通知される
    canvas3d.addMouseListener(new PickTestAdapter(this));

    BranchGroup scene = createSceneGraph();

    SimpleUniverse u = new SimpleUniverse(canvas3d);

    u.getViewingPlatform().setNominalViewingTransform();

    u.addBranchGraph(scene);
  }

  public PickSelectionFeedback() {
    initialize3D();
  }

  public static void main(String[] args) {
    PickSelectionFeedback me = new PickSelectionFeedback();
    MainFrame frame = new MainFrame(me, 500, 500);
    me.init();
    me.start();
  }
}

class PickTestAdapter extends MouseAdapter {
  PickSelectionFeedback adaptee = null;
  
  public PickTestAdapter(PickSelectionFeedback adaptee) {
    this.adaptee = adaptee;
  }
  
  public void mouseClicked( MouseEvent e ){
    Node node = adaptee.pickNode(e.getX(), e.getY());
    if (node != null) {
      String data = (String)node.getUserData();
      System.out.println("UserData=" + data);
    }
  }
}
=================================================================

------------------------------------------------------------------
遠藤靖之 (えんどう やすゆき) <yasuyuki@xxxxxxxxxx>
http://www.freepage.total.co.jp/jfriends/ (Java互助会ホームページ)
株式会社タイムインターメディア 情報通信サービス部 TEL 03-5362-9009
〒162-0065 新宿区住吉町3-11 新宿スパイアビル      FAX 03-5362-9008