Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Java 3D / April 2005

Tip: Looking for answers? Try searching our database.

How to get the x and y position of mouse clicked using JAVA3D

Thread view: 
waluan - 15 Apr 2005 16:59 GMT
I have been trying to implement Reversi(a.k.a. Othello) using Java3D for a
final project of my computer lab course, every thing was going well until i
got stuck. My problem is that i cant seem to find a way to get the X & Y
Coordinate of the mouse click. Because i will need it when the user clickes
i could know on which cell did he/she click on so the game could proceed.

I have tryed using MouseListeners, and MouseMotionListeners and in the body
of MouseClicked i wrote System.exit(1)'just for a check if it works', but
it failed i mean it compiled without errors but when i clicked the left
mouse click nothing happened and even when i changed the body of
mouseClicked so that when  the user clicks it will print  "mouse Clicked!"
on the console window instead of System.exit(1), nothing happened also.

I have tryed asking some friend and some developers but i got no answer.

It would be great if some one has a solution for me.

thank you
Christian Wiech - 16 Apr 2005 12:03 GMT
Sounds like you need picking. It's part of the api. In 3d a ray is projected
into the space and you will get back all elements that intersected that
(invisible) ray (e.g. your reversi cells if they are instances of Shape3D).

an example from an old project and poorly documented:

public class FPPicker extends Behavior
{
   public final String ACTION_COMMAND = "VertexPicked";
   
   protected PickCanvas pickCanvas;

   protected WakeupOnAWTEvent wakeupCriterion;

   protected int x;
   protected int y;

   protected FPPickListener fpp;
   
   /** Creates a new instance of FPPicker */
   public FPPicker(Canvas3D canvas, BranchGroup scene)
   {
       //FPPListenerList = new ArrayList();
       pickCanvas = new PickCanvas(canvas, scene);
       // Objects within this disstance from the mouse location will be
picked.
       pickCanvas.setTolerance(3.0f);
       // We want to get info (indices of points) from the intersected
geometry (the PointArray).
       pickCanvas.setMode(PickCanvas.BOUNDS);
   }
   
   public void initialize()
   {
       // Wake up when a mouse button is clicked
       wakeupCriterion = new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED);
       wakeupOn(wakeupCriterion);
   }

   public void processStimulus(Enumeration criteria)
   {
       while (criteria.hasMoreElements())
       {
           WakeupCriterion wakeup =
(WakeupCriterion)criteria.nextElement();
           if (wakeup instanceof WakeupOnAWTEvent)
           {
               AWTEvent[] events =
((WakeupOnAWTEvent)wakeup).getAWTEvent();
               if (events.length > 0 && events[events.length-1] instanceof
MouseEvent)
               {
                   MouseEvent event = (MouseEvent)events[events.length-1];
                   int id  = event.getID();
                   int mod = event.getModifiers();
                   Point3d closestVertexCoords;
                   PickResult[] pickResult2;
                   int type = -1;
                   if (event.getModifiers() == MouseEvent.BUTTON1_MASK)
type = FPPickEvent.POINT;
                   if (event.getModifiers() == MouseEvent.BUTTON3_MASK)
type = FPPickEvent.END_POINT;
                   if (type == -1) return;
                   
                   // The location of the mouse event.
                   x = event.getX();
                   y = event.getY();

                   // Pick the closest node
                   pickCanvas.setShapeLocation(x, y);
                   Point3d eyePos = pickCanvas.getStartPosition();
                   pickCanvas.setMode(PickCanvas.BOUNDS);
                   try
                   {
                       //pickResult = pickCanvas.pickClosest();
                       pickResult2 = pickCanvas.pickAllSorted();
                       for (int k = 0; k < pickResult2.length; k++)
                       {
                           if (pickResult2 != null && pickResult
[k].getNode(PickResult.PRIMITIVE) instanceof FAPObject)
                           {
                               //Yeah this is an existing Shape got to find
more shapes
                               FAPObject fapTemp = (FAPObject) pickResult
[k].getNode(PickResult.PRIMITIVE);
                               fireFPPicked(((FAPObject) pickResult
[k].getNode(PickResult.PRIMITIVE)).getID(), type);
                               wakeupOn(wakeupCriterion);
                               return;
                           }
                       }
                   }
                   catch (CapabilityNotSetException er)
                   {
                       er.printStackTrace();
                   }
                   
               }
           }
       }
       wakeupOn(wakeupCriterion);
   }
   
   
   public void addFPPickListener(FPPickListener l) {
       //FPPListenerList.add(l);
       fpp = l;
   }
   
   
   public void removeFPPickListener(FPPickListener l) {
       //FPPListenerList.remove(l);
       fpp = null;
   }
   
   protected void fireFPPicked(int fpID, int type)
   {
       /*if (FPPListenerList == null) return;
       FPPickListener fppl;
       Iterator it = FPPListenerList.iterator();
       while (it.hasNext())
       {
           fppl = (FPPickListener) it.next();
           FPPickEvent e = new FPPickEvent(this, fpID, type);
           fppl.fpPicked(e);
       }*/
       if (fpp != null)
       {
           FPPickEvent e = new FPPickEvent(this, fpID, type);
           fpp.fpPicked(e);
       }
   }
}

Hope that helps.

Cheers

Christian
deimy l - 26 Apr 2005 10:24 GMT
May i know where is your class file for FPPickListener please?

My problem is totally same as waluan faced.

I could make the mouse interaction such as Zoom in Zoom Out, Translation
and Rotate only. But i could not use the mouse click and show out the x y
coordinates as well as set tool tip text.

Even thought i have tried the add mouse listener but still failed to do it.
It did show the x y coordinate on the console.
Christian Wiech - 27 Apr 2005 13:20 GMT
Try to understand the chapter Picking in j3d_tutorial_ch4.pdf from SUNs
website.

http://java.sun.com/products/java-media/3D/collateral/index.html#tutorial

My FPPickListener is just a standard listener that has nothing to do with
main process of picking. But here it is:

import java.util.EventListener;

public interface FPPickListener  extends EventListener
{
   
   public void fpPicked(FPPickEvent e);
   
}

The picked objects are already identified in pickResult2 (ordered by
intersection with the pickray) and then I try to identify them with
instanceof. In my case: as soon as I have found a shape that is instanceof
FAPObject I fire up my FPPickListener so that my GUI surounding the
Canvas3D can react to this result.

Cheers

Christian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.