# MouseListener in Java3D



## KA (17. Aug 2008)

Ich habe einen MouseListener in einem SimpleUniverse eingebaut.
In diesen SimpleUniverse habe ich eine Sphere getan.
Nun will ich, dass wenn ich klicke, meine Kugel sich z.B. nach rechts um 0.1 Meter bewegt:


```
package Player;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.TransformGroup;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Main extends Applet implements MouseListener {
	private static final long serialVersionUID = 1L;
	ArrayList<Agent> agents = new ArrayList<Agent>();
	Player player;
	SimpleUniverse u;

	public static void main(String[] args) {
		new MainFrame(new Main(), 800, 600);
	}

	public Main() {
		addMouseListener(this);
		agents.add(new Player());

		GraphicsConfiguration config = SimpleUniverse
				.getPreferredConfiguration();
		Canvas3D c = new Canvas3D(config);
		u = new SimpleUniverse(c);
		u.getViewingPlatform().setNominalViewingTransform();
		u.getViewer().getView().setMinimumFrameCycleTime(4);
		setLayout(new BorderLayout());
		add("Center", c);
		c.addMouseListener(this);
		c.requestFocus();
		BranchGroup inhalt = makeInhalt();
		u.addBranchGraph(inhalt);

	}

	public BranchGroup makeInhalt() {
		BranchGroup obj_root = new BranchGroup();
		TransformGroup obj_trans = new TransformGroup();
		for (int i = 0; i < agents.size(); i++)
			obj_trans.addChild(agents.get(i).getInhalt());
		obj_root.addChild(obj_trans);
		return obj_root;
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			// Was kommt dann hier hin?
		}
		if (e.getButton() == MouseEvent.BUTTON2) {
		}
	}

	public void mouseReleased(MouseEvent e) {
	}
}
```


```
package Player;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Sphere;

public class Player implements Agent {

	public TransformGroup getInhalt() {
		TransformGroup obj_root = new TransformGroup();
		TransformGroup obj_trans = new TransformGroup();
		obj_trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ
				& TransformGroup.ALLOW_TRANSFORM_WRITE);

		Appearance sphere_app = new Appearance();
		ColoringAttributes ca = new ColoringAttributes();
		ca.setColor(1, 0, 0);

		sphere_app.setColoringAttributes(ca);

		Sphere sphere = new Sphere(0.3f, sphere_app);

		obj_trans.addChild(sphere);
		Transform3D trans_sphere = new Transform3D();
		Vector3f punkt = new Vector3f(0, 0, 0);
		trans_sphere.setTranslation(punkt);
		obj_trans.setTransform(trans_sphere);
		obj_root.addChild(obj_trans);
		return obj_root;
	}

}
```

Das sind die Klassen...
Ist es sehr schlecht? Oder okay? [/code]


----------



## Marco13 (17. Aug 2008)

Eigentlich verwendet man in Java3D keine Listener, sondern "Behaviors". Mit Listenern _geht_ es wohl auch, aber eigentlich sind die Behviors für sowas gedacht. (Siehe z.B. auch hier http://javaboutique.internet.com/resources/books/java3d/java3d5_1.html bei "User inputs").

Um die Frage im Code andeutungsweise (!!!) zu beantworten

```
public class Player implements Agent {

      Transform3D trans_sphere = new Transform3D(); // Hier hin!

   public TransformGroup getInhalt() {
...
      obj_trans.addChild(sphere);
      // --------------------- Hier weg: ---- Transform3D trans_sphere = new Transform3D();
      Vector3f punkt = new Vector3f(0, 0, 0);
      trans_sphere.setTranslation(punkt);
      obj_trans.setTransform(trans_sphere);
....
   }

}


....
public void mousePressed(MouseEvent e) {
      if (e.getButton() == MouseEvent.BUTTON1) {
         player.trans_sphere.setTranslation(new Vector3f(0.1f, 0,0)); // Translation setzen
      }
      if (e.getButton() == MouseEvent.BUTTON2) {
      }
   }
```


----------



## KA (21. Aug 2008)

ok, da hab ich in die falsche Richtung gedacht...

aber es funktioniert trotzdem nicht...

ich habe ein tutorial von sun gefunden und werde es lesen... in kapitel 3-4 kommt Interaction vor.. da werden bevaviors wohl vorkommen, danke:

```
package Player;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Main extends Applet implements MouseListener {
	private static final long serialVersionUID = 1L;
	ArrayList<Agent> agents = new ArrayList<Agent>();
	Player player;
	SimpleUniverse u;

	public static void main(String[] args) {
		new MainFrame(new Main(), 800, 600);
	}

	public Main() {
		addMouseListener(this);
		agents.add(player = new Player());

		GraphicsConfiguration config = SimpleUniverse
				.getPreferredConfiguration();
		Canvas3D c = new Canvas3D(config);
		u = new SimpleUniverse(c);
		u.getViewingPlatform().setNominalViewingTransform();
		u.getViewer().getView().setMinimumFrameCycleTime(4);
		setLayout(new BorderLayout());
		add("Center", c);
		c.addMouseListener(this);
		c.requestFocus();
		BranchGroup inhalt = makeInhalt();
		inhalt.compile();
		u.addBranchGraph(inhalt);
	}

	public BranchGroup makeInhalt() {
		BranchGroup obj_root = new BranchGroup();
		TransformGroup obj_trans = new TransformGroup();
		for (int i = 0; i < agents.size(); i++)
			obj_trans.addChild(agents.get(i).getInhalt());
		obj_root.addChild(obj_trans);
		return obj_root;
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1) {
			Player.x += 0.1f;
			System.out.print(Player.x + "  " + Player.y + "  " + Player.z + "\n");
			player.trans_sphere.setTranslation(new Vector3f(Player.x, Player.y,
					Player.z));
		}
		if (e.getButton() == MouseEvent.BUTTON2) {
		}
	}

	public void mouseReleased(MouseEvent e) {
	}
}
```


```
package Player;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Sphere;

public class Player implements Agent {
	
	public Transform3D trans_sphere = new Transform3D();
	
	static float x, y, z;
	
	public Player() {
		x = y = z = 0;
	}

	public TransformGroup getInhalt() {
		TransformGroup obj_root = new TransformGroup();
		TransformGroup obj_trans = new TransformGroup();
		obj_trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ
				& TransformGroup.ALLOW_TRANSFORM_WRITE);

		Appearance sphere_app = new Appearance();
		ColoringAttributes ca = new ColoringAttributes();
		ca.setColor(1, 0, 0);

		sphere_app.setColoringAttributes(ca);

		Sphere sphere = new Sphere(0.3f, sphere_app);

		obj_trans.addChild(sphere);
		Vector3f punkt = new Vector3f(x, y, z);
		trans_sphere.setTranslation(punkt);
		obj_trans.setTransform(trans_sphere);
		obj_root.addChild(obj_trans);
		return obj_root;
	}


}
```

wenn ich die player nicht static mache, kommt eine Fehlermeldung... was mache ich hier falsch?


----------



## Marco13 (21. Aug 2008)

Player klein schreiben. Es geht ja um das player-Objekt und nicht um die Klasse Player.


----------

