Skip to main content

Adding new object at mouse location

1 reply [Last post]
Joined: 2006-02-17
Points: 0

Hopefully someone can point in the right direction.

I am trying to add a sphere into the canvas3d but not at the origin. I want it where I click the mouse within the canvas. I figure I would need to create a transform group that translate according to the mouse coordinate and apply it to the sphere before inserting into a branch graph.

My problem is that I am not sure how to get the mouse coordinate. The sphere is inserted when I click on a menu option from my swing gui. so far so good but where do I insert my mouse listener and make it a prerequisite for the sphere insertion.

Any suggestion would be much appreciated.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-05-05
Points: 0


I'm not an expert on this kind of thing but I've done something similar before. In the past I've extended a Behavior class to make my own 'PickBehavior'. In the class you can include a PickCanvas and pass it your Canvas3D and the BranchGroup containing your mouse TransformGroups etc. Then define the methods:

> public void initialize() {
> wakeupOn (new WakeupOnAWTEvent
> MouseEvent.MOUSE_CLICKED));
> }


> public void processStimulus (Enumeration criteria) {
> WakeupCriterion wakeup;
> AWTEvent[] event;

> while (criteria.hasMoreElements()) {
> wakeup = (WakeupCriterion) criteria.nextElement();

Then if you use something like:

> event = ( (WakeupOnAWTEvent) wakeup).getAWTEvent();

you can determine the type of MouseClickedEvent and get the x and y from the Event:

> x = ( (MouseEvent) event[i]).getX();
> y = ( (MouseEvent) event[i]).getY();
> pickCanvas.setShapeLocation(x, y);

Add your PickBehavior as a child of the root BranchGroup of your SimpleUniverse (or whatever you're using).

This probably doesn't make that much sense on its own but if you read up on the different bits it might be useful? :)