[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Java3Djp:00604] LineTest.java
---広告---------------------------------------------------------------
広告掲載募集中 → http://www.dns-ml.co.jp/ad-proto.html
----------------------------------------------------------------------
MLならDNS(非公開OK) → http://www.dns-ml.co.jp/ml+.html
----------------------------------------------------------------------
メルマガもDNS(非公開OK)→ http://www.dns-ml.co.jp/mm-index.html
----------------------------------------------------------------------
あまりきれいな実装ではありませんが、マウスクリックした点をつないで
LineStripArray を描画するサンプルを書きましたので投稿します。
クリック点は最大 50 です。
// Java 3Dテスト用アプレット
// LineTest.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
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.ColorCube;
public class LineTest extends Applet {
public final int MAX_VERTICES = 50;
public int vertexCount = 0;
public Point3d[] vertices = new Point3d[MAX_VERTICES];
boolean debug = true;
Canvas3D canvas = null;
SimpleUniverse universe = null;
BranchGroup scene = null;
TransformGroup objTrans = null;
Shape3D shape = null;
LineStripArray geometry = null;
public LineTest() {
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config);
this.setLayout(new BorderLayout());
this.add(canvas, "Center");
canvas.addMouseListener(new MyMouseAdapter(this));
universe = new SimpleUniverse(canvas);
universe.getViewingPlatform().setNominalViewingTransform();
scene = createSceneGraph();
universe.addBranchGraph(scene);
}
private BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
objRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
objRoot.setCapability(Group.ALLOW_CHILDREN_READ);
objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);
objRoot.setCapability(BranchGroup.ALLOW_DETACH);
for (int i=0; i < vertices.length; i++) {
vertices[i] = new Point3d();
}
int[] lineStripCounts = { vertices.length };
geometry =
new LineStripArray(vertices.length, GeometryArray.COORDINATES, lineStripCounts);
geometry.setCoordinates(0, vertices);
shape = new Shape3D(geometry);
shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
objTrans = new TransformGroup();
objTrans.addChild(shape);
objRoot.addChild(objTrans);
return objRoot;
}
public void addPoint(int xpos, int ypos) {
vertexCount++;
if (vertexCount > MAX_VERTICES) vertexCount = MAX_VERTICES;
Transform3D motion=new Transform3D();
Point3d eyePosn = new Point3d();
Point3d mousePosn = new Point3d();
Vector3d mouseVec=new Vector3d();
canvas.getCenterEyeInImagePlate(eyePosn);
canvas.getPixelLocationInImagePlate(xpos,ypos,mousePosn);
canvas.getImagePlateToVworld(motion);
if (debug) {
System.out.println("mouse position " + xpos + " " + ypos);
System.out.println("before, mouse " + mousePosn + " eye " + eyePosn);
}
motion.transform(eyePosn);
motion.transform(mousePosn);
mouseVec.sub(mousePosn, eyePosn);
mouseVec.normalize();
if (debug) {
System.out.println("vertexCount=" + vertexCount);
System.out.println(motion + "\n");
System.out.println("after, mouse " + mousePosn + " eye " + eyePosn +
" mouseVec " + mouseVec);
}
universe.getLocale().removeBranchGraph(scene);
vertices[vertexCount - 1].x = mousePosn.x;
vertices[vertexCount - 1].y = mousePosn.y;
vertices[vertexCount - 1].z = mousePosn.z;
if (vertexCount > 1) {
int[] lineStripCounts = { vertexCount };
geometry =
new LineStripArray(vertexCount, GeometryArray.COORDINATES, lineStripCounts);
Point3d[] subVertices = new Point3d[vertexCount];
for (int i=0; i < vertexCount; i++) {
subVertices[i] = vertices[i];
}
geometry.setCoordinates(0, subVertices);
shape.setGeometry(geometry);
}
universe.addBranchGraph(scene);
}
public static void main(String[] args) {
LineTest applet = new LineTest();
MainFrame frame = new MainFrame(applet, 500, 500);
}
}
class MyMouseAdapter implements MouseListener {
LineTest applet = null;
MyMouseAdapter(LineTest applet) {
this.applet = applet;
}
public void mousePressed( MouseEvent e ){}
public void mouseReleased( MouseEvent e ){}
public void mouseEntered( MouseEvent e ){}
public void mouseExited( MouseEvent e ){}
public void mouseClicked( MouseEvent e ){
applet.addPoint(e.getX(), e.getY());
}
}
--
えんどう やすゆき <yasuyuki@xxxxxxxxxx>
http://www.javaopen.org/jfriends/ (Java互助会ホームページ)