If you just to want to draw the line on the canvas,
you can simply overload the postwap() method of your canvas 3D( create your own
canvas which derives from Canvas3D) inside which you draw the line
This method is called each time the java3d scene is
repainted.
----- Original Message -----
Sent: Tuesday, September 18, 2001 9:11
AM
Subject: [JAVA3D] Urgent Need for
help
hi,
i am a graduate
student and have taken up a project in java and java3d. but being new to
J3D i am not able to solve some problems.
[1] I have added a Canvas3D
to a panel and i want to draw a simple line on the Canvas. i am using my
own class for line (myLine2D) and converting it to Shape3D using a method
'getshape()' and adds it to scenegraph using method 'addShapeObject()'.
Though the object is added to the TG node in the content branch, the Canvas
is not displaying it. i am herewith sending the source
file contents.
[2] Also when the Canvas3D object is added to the
panel, the menus in the menubars are not displayed as the opaque canvas
overlaps them.
please can anyone help me as early as possible?
thanks!
------------------------------------------------------------------------- ------------ [myLine2D.java] package
jpuf.geometry;
import javax.vecmath.*; import
javax.media.j3d.*;
public class myLine2D { private float
x1,y1,x2,y2;
public myLine2D(int x1,int y1,int x2,int
y2) { this.x1=(float)x1;
this.y1=(float)y1;
this.x2=(float)x2; this.y2=(float)y2;
}
public Shape3D getShape() {
Shape3D shape; float[] verts=
{
x1,y1,0.0f, x2,y2,0.0f
}; /* int[]
stripVertexCount={1,1}; //
System.out.println("def:stripvc"); LineStripArray lsa =
new LineStripArray(2,LineStripArray.COORDINATES,stripVertexCount); //
System.out.println("setcoords");
lsa.setCoordinate(0,verts); System.out.println("No. of
Strips : "+lsa.getNumStrips()); System.out.println("No.
of Vertices : "+lsa.getVertexCount());
System.out.println("("+verts[0]+","+verts[1]+","+verts[2]+")");
System.out.println("("+verts[3]+","+verts[4]+","+verts[5]+")");
shape=new Shape3D(lsa,new Appearance());*/ //
System.out.println("def:shape3d"); LineArray la=new
LineArray(2,GeometryArray.COORDINATES);
la.setCoordinate(0,verts); shape=new Shape3D(la,new
Appearance()); return shape;
} }
------------------------------------------------------------------------- ------------ [Model.java]
package
jpuf;
import java.awt.*; import javax.swing.*; import
jpuf.geometry.*; // OUR PACKAGE import
javax.vecmath.*; import javax.media.j3d.*;
public class Model
{
private VirtualUniverse VU; private View
v; private TransformGroup TG; private BranchGroup
BG; private Locale LC; private Canvas3D
c3d;
public Model(JPanel c)
{ VirtualUniverse universe=new
VirtualUniverse();
this.VU=universe; Locale locale=new
Locale(universe); this.LC=locale;
locale.addBranchGraph(GenerateRightSubtree(c));
locale.addBranchGraph(GenerateLeftSubtree()); }
private BranchGroup GenerateRightSubtree(JPanel c)
{ BranchGroup root=new
BranchGroup(); Transform3D t=new
Transform3D(); t.set(new
Vector3f(0.0f,0.0f,10.0f)); TransformGroup TG=new
TransformGroup(t);
ViewPlatform vplat=new
ViewPlatform(); PhysicalBody pbody = new
PhysicalBody(); PhysicalEnvironment penv=new
PhysicalEnvironment();
TG.addChild(vplat);
root.addChild(TG);
View view=new
View(); this.v=view; this.c3d=new
Canvas3D(null); //
System.out.println(this.c3d.hashCode());
this.c3d.setBackground(Color.white);
this.c3d.setForeground(Color.black);
c.add(c3d);
view.addCanvas3D(this.c3d); //
System.out.println(this.c3d); //
System.out.println(this.c3d.isValid()); //
System.out.println(this.c3d.isVisible());
view.attachViewPlatform(vplat);
view.setPhysicalBody(new PhysicalBody());
view.setPhysicalEnvironment(new PhysicalEnvironment());
root.setCapability(BG.ALLOW_CHILDREN_READ);
return(root); }
private BranchGroup
GenerateLeftSubtree() { BranchGroup root=new
BranchGroup(); Transform3D t=new
Transform3D(); TransformGroup TG=new
TransformGroup(); this.TG=TG; /* BoundingSphere
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); (is it
required?) TG.setBounds(bounds);
*/ root.addChild(TG);
this.BG=root;
root.setCapability(BG.ALLOW_CHILDREN_READ);
root.setCapability(root.ALLOW_CHILDREN_WRITE);
root.setCapability(root.ALLOW_DETACH);
return(root); }
// [THIS METHOD ADDS A SHAPE3D OBJECT
FROM OUTSIDE TO THE // TRANSFORMGROUP NODE OF LEFT SUBTREE OF
THE PREVIOUSLY // CREATED SCENEGRAPH]
public
void addShapeObject(Shape3D shape) {
this.c3d.stopRenderer(); //
System.out.println("addshapeobj:gotshape"+shape);
shape.setPickable(true);
shape.setCapability(shape.ALLOW_GEOMETRY_WRITE); //
System.out.println("Live : "+this.TG.isLive());
this.LC.removeBranchGraph(this.BG); //
System.out.println("Live : "+this.TG.isLive());
this.TG.addChild(shape); //
System.out.println("attached:shape3d");
this.LC.addBranchGraph(this.BG);
this.c3d.startRenderer();
/////////////////////////////////////////////////////
System.out.println(this.c3d.hashCode());
///////////////////////////////////////////////////// //
System.out.println("Renderer : "+this.c3d.isRendererRunning()+"\tCanvas
: "+this.c3d.isValid()); //
System.out.println("Canvas3D size :
"+this.c3d.getSize()); return; }
// [for multiple windows] public void newCanvas(Component
c) { this.c3d=new
Canvas3D(null);
v.addCanvas3D(this.c3d);
System.out.println(this.c3d); }
// [for multiple
windows] public Canvas3D
getCanvas() // returns canvas to
main file for adding in a Jpanel {
this.c3d.setBackground(Color.blue);
this.c3d.setForeground(Color.white); return
this.c3d; }
public void setCanvas(Canvas3D
c) // This canvas is passed from main file .. and
it overrides the original canvas created above
{ this.c3d=c;
} }
|