import com.sun.j3d.utils.picking.behaviors.PickRotateBehavior;
import com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior;
import com.sun.j3d.utils.picking.behaviors.PickZoomBehavior;
....
BranchGroup objWurzel = new BranchGroup();
private TransformGroup erstelleBehaviorsGruppe()
{
TransformGroup Gruppe = new TransformGroup();
Gruppe.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
Gruppe.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Gruppe.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
pickzoom = new PickZoomBehavior(objWurzel, canves3D, new BoundingSphere(new Point3d(), 100));
pickDreh = new PickRotateBehavior(objWurzel, canves3D, new BoundingSphere(new Point3d(), 100));
picktrans = new PickTranslateBehavior(objWurzel, canves3D, new BoundingSphere(new Point3d(), 100));
pickDreh.setTolerance(0f);
Gruppe.addChild(pickDreh);
Gruppe.addChild(pickzoom);
Gruppe.addChild(picktrans);
return Gruppe;
}
ObjWurzel.addChild(erstelleBehaviorsGruppe());
//der BoxTG die Transformationen mit der Maus erlauben
BoxTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
BoxTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
BoundingBox boundBox=new BoundingBox (new Point3d(-1000,-1000,-1000),new Point3d(1000,1000,1000));
//rotieren mit der linken Maustaste
MouseRotate behavior = new MouseRotate(BoxTG);
behavior.setTransformGroup(BoxTG);
behavior.setSchedulingBounds(boundBox);
BoxTG.addChild(behavior);
//verschieben mit rechter Maustaste
MouseTranslate mouseTranslate= new MouseTranslate(BoxTG);
mouseTranslate.setSchedulingBounds(boundBox);
BoxTG.addChild(mouseTranslate);
//zoomen mit mittlerer Maustaste
MouseZoom mouseBeh2=new MouseZoom(BoxTG);
mouseBeh2.setSchedulingBounds(boundBox);
BoxTG.addChild(mouseBeh2);
//Startwerte für die Generierung der Würfel im festgelegtem Layout(bis 500 Knoten)
x=(float)-0.5;
y=(float)-0.5;
n= anzahlWuerfel;
//Generierung der Knoten
for(i=0; i<n; i++){
switch(i){
case 50: { y= (float)(y + 0.025);
x=(float)-0.5; break;} //x Wert wird für nächste Zeile zurückgesetzt
case 100: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
case 150: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
case 200: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
case 250: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
case 300: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
case 350: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
case 400: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
case 450: { y= (float)(y + 0.025);
x=(float)-0.5; break;}
}
TransformGroup BoxTG = new TransformGroup();
Transform3D BoxT3D = new Transform3D();
BoxT3D.setTranslation(new Vector3f((float)x,(float)y,0f)); // x und y damit die Knoten als "Feld" ausgegeben werden
BoxTG.setTransform(BoxT3D);
BoxTG.addChild(new Box(0.01f,0.01f,0.01f, BoxAppearance));
x= (float) (x+ 0.025);
BoxTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
BoxTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
BoxTG.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
//PickTranslateBehavior
PickTranslateBehavior pickT = new PickTranslateBehavior(rootBG, c, BigBounds);
rootBG.addChild(pickT);
rootBG.addChild(BoxTG);
}
Hast du die Objekt Skaliert(setScale()) ?
Ich vermute mal das das Objekt einfach aus dem Sichtfeld verschwindet weil es beim anklicken zu groß oder zu klein geworden ist.
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.mouse.MouseBehavior;
import com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback;
import com.sun.j3d.utils.behaviors.picking.*;
import javax.media.j3d.*;
public class PickTranslateBehavior extends PickMouseBehavior
implements MouseBehaviorCallback
{
MouseTranslate translate;
int pickMode = PickObject.USE_BOUNDS;
private PickingCallback callback = null;
private TransformGroup currentTG;
public PickTranslateBehavior(BranchGroup root, Canvas3D canvas, Bounds bounds)
{
super (canvas, root, bounds);
translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP);
//**********************************//
// Hier stelle ich die geschwindigkeit ein!!!
// *********************************//
translate.setFactor(0.01);
translate.setTransformGroup(currGrp);
currGrp.addChild(translate);
translate.setSchedulingBounds(bounds);
this .setSchedulingBounds(bounds);
}
public PickTranslateBehavior(BranchGroup root, Canvas3D canvas,
Bounds bounds, int pickMode) {
super (canvas, root, bounds);
translate = new MouseTranslate(MouseBehavior.MANUAL_WAKEUP);
translate.setTransformGroup(currGrp);
currGrp.addChild(translate);
translate.setSchedulingBounds(bounds);
this .setSchedulingBounds(bounds);
this .pickMode = pickMode;
}
public void setPickMode(int pickMode) {
this .pickMode = pickMode;
}
public int getPickMode() {
return pickMode;
}
public void updateScene(int xpos, int ypos) {
TransformGroup tg = null;
if (!mevent.isAltDown() && mevent.isMetaDown()) {
tg = (TransformGroup) pickScene.pickNode(pickScene
.pickClosest(xpos, ypos, pickMode),
PickObject.TRANSFORM_GROUP);
//Check for valid selection.
if ((tg != null)
&& (tg
.getCapability(TransformGroup.ALLOW_TRANSFORM_READ))
&& (tg
.getCapability(TransformGroup.ALLOW_TRANSFORM_WRITE))) {
translate.setTransformGroup(tg);
translate.wakeup();
currentTG = tg;
} else if (callback != null)
callback
.transformChanged(PickingCallback.NO_PICK, null);
}
}
public void transformChanged(int type, Transform3D transform) {
callback.transformChanged(PickingCallback.TRANSLATE, currentTG);
}
public void setupCallback(PickingCallback callback) {
this.callback = callback;
if (callback == null)
translate.setupCallback(null);
else
translate.setupCallback(this);
}
}