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

[Java3Djp:00771] Java 3D 1.1.2 でのLineのpick



=◆ソナム亭◆==  販売代理店募集中(限定50件) ============
本場韓国風のピリッとジューシーな食感!! 
今までのチキンのイメージが変わります。
是非、一度ご賞味下さい。 現在代理店募集中(限定50件)を行っております。
=========== http://www.matsuronet.ne.jp/somamutei/======

えんどうです。

>2つの線(LineArray)を表示して、クリックすると色が変わるソースを添付しておきます。

超遠隔リプライですが、
Java 3D 1.1.2 OpenGL Win32 では、PickObject.USE_GEOMETRY, USE_BOUNDS ともに
Line の pick に成功します。

2月からアーカイブが更新されていないようなので、つきやまさんのソースを再掲します。

  1 import java.applet.Applet;
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 import java.awt.event.WindowAdapter;
  5 import com.sun.j3d.utils.applet.MainFrame;
  6 import com.sun.j3d.utils.behaviors.picking.PickObject;
  7 import com.sun.j3d.utils.universe.*;
  8 import javax.media.j3d.*;
  9 import javax.vecmath.*;
 10 
 11 public class lineSample extends Applet{
 12     
 13     public Canvas3D c;
 14     public BranchGroup objRoot;
 15     public TransformGroup objTrans;
 16     
 17         LineArray line_g;
 18         LineArray line2_g;
 19 
 20  public BranchGroup createSceneGraph() {
 21 
 22     objRoot = new BranchGroup();
 23     
 24     objTrans = new TransformGroup();
 25     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
 26     objRoot.addChild(objTrans);
 27 
 28     /* Line */
 29     //Geometry
 30     line_g = new LineArray(6, LineArray.COORDINATES | LineArray.COLOR_3);
 31     line_g.setCapability( LineArray.ALLOW_COLOR_WRITE );
 32     line_g.setCapability( LineArray.ALLOW_INTERSECT );
 33   
 34     line2_g = new LineArray(6, LineArray.COORDINATES | LineArray.COLOR_3);
 35     line2_g.setCapability( LineArray.ALLOW_COLOR_WRITE );
 36     line2_g.setCapability( LineArray.ALLOW_INTERSECT );
 37     
 38     //Vertex
 39     Point3d v1  = new Point3d(0.0, -0.25, 0.0);
 40     Point3d v2  = new Point3d(0.25, -0.25, 0.0);
 41     Point3d v3  = new Point3d(0.25, -0.5, 0.0);
 42     Point3d v4  = new Point3d(0.5, -0.5, 0.0);
 43     Point3d[] vertexs = {v1, v2, v2, v3, v3, v4};
 44     line_g.setCoordinates(0, vertexs);
 45     
 46     Point3d v01 = new Point3d(0.0, 0.25, 0.0);
 47     Point3d v02 = new Point3d(0.25, 0.25, 0.0);
 48     Point3d v03 = new Point3d(0.25, 0.0, 0.0);
 49     Point3d v04 = new Point3d(0.5, 0.0, 0.0);
 50     Point3d[] vertexs2 = {v01, v02, v02, v03, v03, v04};
 51     line2_g.setCoordinates(0, vertexs2);
 52     
 53     //Color
 54     Color3f line_c  = new Color3f(0.0f, 1.0f, 0.0f);
 55     Color3f[] colors = {line_c, line_c, line_c, line_c, line_c, line_c};
 56     line_g.setColors(0, colors);
 57     line2_g.setColors(0, colors);
 58     
 59     //Shape
 60     Shape3D line_o = new Shape3D(line_g);
 61   line_o.setCapability(Shape3D.ENABLE_PICK_REPORTING);
 62   line_o.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
 63   line_o.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
 64   line_o.setUserData("line_off");
 65     
 66     Shape3D line2_o = new Shape3D(line2_g);
 67   line2_o.setCapability(Shape3D.ENABLE_PICK_REPORTING);
 68   line2_o.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
 69   line2_o.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
 70     line2_o.setUserData("line_off");
 71     
 72     //Appearance
 73     Appearance line_a = new Appearance();
 74     LineAttributes la = new LineAttributes(1.0f, LineAttributes.PATTERN_SOLID, false);
 75     line_a.setLineAttributes(la);
 76     line_o.setAppearance(line_a);
 77     line2_o.setAppearance(line_a);
 78         
 79     //Add SG
 80     objTrans.addChild(line2_o);
 81     objTrans.addChild(line_o);
 82     
 83     return objRoot;
 84  }
 85 
 86  public lineSample() {
 87 
 88     setLayout(new BorderLayout());
 89     c = new Canvas3D(null);
 90     add("Center", c);
 91 
 92     BranchGroup scene = createSceneGraph();
 93     scene.setCapability( BranchGroup.ALLOW_BOUNDS_READ );
 94     
 95     //Mouse Listener
 96     MListener mlistener = new MListener(c, objRoot);
 97     c.addMouseListener(mlistener);
 98     
 99     SimpleUniverse u = new SimpleUniverse(c);
100   u.getViewingPlatform().setNominalViewingTransform();
101     u.addBranchGraph(scene);
102 
103  }
104 
105     public void printAll(Graphics g) {
106         int width = getSize().width;
107         int height = getSize().height;
108         g.setClip(0, 0, width, height);
109         super.printAll(g);
110     }
111 
112  public static void main(String[] args) {
113     new MainFrame(new lineSample(), 256, 256);
114  }
115 
116 }
117 
118 
119 class MListener extends MouseAdapter{
120     
121     Canvas3D canvas;
122     BranchGroup objRoot;
123     
124     public MListener(Canvas3D c, BranchGroup or){
125         super();
126         canvas = c;
127         objRoot = or;
128     }
129     
130     public void mousePressed(MouseEvent e){
131         int x1 = e.getX();
132         int y1 = e.getY();
133         
134         System.out.println("mousePressed(x1,y1)=(" + x1 + "," + y1 + ")");
135         
136         /* Line Picking */
137         
138         PickObject pick = new PickObject(canvas,objRoot);System.out.println("no1");
139         
140     //SceneGraphPath path = pick.pickClosest(x1, y1, PickObject.USE_BOUNDS);System.out.println("no2");
141     SceneGraphPath path = pick.pickClosest(x1, y1, PickObject.USE_GEOMETRY);System.out.println("no2");
142     System.out.println("hoge");
143     Shape3D shape = (Shape3D)pick.pickNode(path,PickObject.SHAPE3D);
144     
145     Color3f line_c;
146     
147     if (shape != null){
148             System.out.println("picking!");
149             
150             if(shape.getUserData().equals("line_off")){
151             line_c  = new Color3f(1.0f, 0.0f, 0.0f);
152             shape.setUserData("line_on");
153         }
154         else{
155             line_c  = new Color3f(0.0f, 1.0f, 0.0f);
156             shape.setUserData("line_off");
157         }
158 
159         System.out.println( shape.getUserData() );
160             Color3f[] colors = {line_c, line_c, line_c, line_c, line_c, line_c};
161         ((LineArray)shape.getGeometry()).setColors(0, colors);
162     }
163         
164     
165     }
166 
167 }
-- 
ENDO Yasuyuki <yasuyuki@xxxxxxxxxx>
http://www.javaopen.org/jfriends/ (Japanese Only)