MouseListener -> Jogl,canvas

Hello , its me again :slight_smile:

i add mouselistener and keylistner to my project
and with System.out.println(“Mouse X”+mx+" Y"+my);
i see that it working over the canvasjogl thing…
my question… how can i use the vars from the
mouseMoved function (mx,my) inside the
display function where my 3d stuff is.

much thanks :slight_smile:


public class FSO extends JFrame implements WindowListener,MouseListener,MouseMotionListener,KeyListener
{
...
public FSO()
  {
   ...
     ...	
	  
	  GLU glu = new GLU();
	  canvas = new GLCanvas(cap);
	  canvas.addGLEventListener(new SceneView());
                getContentPane().add(canvas);
 
                this.addWindowListener(this);  
                canvas.addMouseListener(this);
                canvas.addMouseMotionListener(this);
                canvas.addKeyListener(this);
}


class SceneView implements GLEventListener
  {

...My 3d stuff is here..perfect

public void init(GLAutoDrawable arg0) 
    {


public void display(GLAutoDrawable arg0) 
    {
...

HERE i NEED the Mouse X and Y Data and such things from
the mouselistener, how can i import the int mx/ int my from function
below (mouseMoved) to use here??

   }


}


@Override
public void mouseMoved(MouseEvent arg0) {
	// TODO Auto-generated method stub
	int mx=arg0.getX();
	int my=arg0.getY();
	mouse_x = mx;
	
	System.out.println("Maus X"+mx+" Y"+my);
}

This is called picking.

I advise you to search the forum for it you may find interesting posts like:
http://www.java-gaming.org/forums/index.php?topic=18273.0
http://www.java-gaming.org/forums/index.php?topic=13480.0

Jean-Baptiste

Just store the values as class members. You can access the members of an outer class from within an inner class, so the following will work:


public class FSO extends JFrame implements WindowListener,MouseListener,MouseMotionListener,KeyListener
{
  // just define some member variables to hold the last x and y coordinates of the mouse
  int mx;
  int my;
  ...
  public FSO()
  {
   ...
     ...	
	  
	  GLU glu = new GLU();
	  canvas = new GLCanvas(cap);
	  canvas.addGLEventListener(new SceneView());
          getContentPane().add(canvas);
 
          this.addWindowListener(this);  
          canvas.addMouseListener(this);
          canvas.addMouseMotionListener(this);
          canvas.addKeyListener(this);
  }

  class SceneView implements GLEventListener
  {
    ...My 3d stuff is here..perfect

    public void init(GLAutoDrawable arg0) 
    {
    }
    
    public void display(GLAutoDrawable arg0) 
    {
        ...
        // this will work, because inner classes have access to the members of a surrounding class
        float doSomeStuff= mx*0.5+my*0.3+bar;
     }
  }

  @Override
  public void mouseMoved(MouseEvent arg0) {
	// just store the values in the outer class
	mx=arg0.getX();
	my=arg0.getY();
	mouse_x = mx;
	
	System.out.println("Maus X"+mx+" Y"+my);
   }
}


If you want to encapsulate the SceneView from the FSO class, add setMX() and setMY() methods to your SceneView class. Then you can store the instance of SceneView in a variable and set the values in your MouseListener. This would also work, if SceneView is not an inner class:


public class FSO extends JFrame implements WindowListener,MouseListener,MouseMotionListener,KeyListener
{
  // Hold an instance of your SceneView implementation
  SceneView view;
  ...
  public FSO()
  {
   ...
     ...	
	  
	  GLU glu = new GLU();
	  canvas = new GLCanvas(cap);
          view = new SceneView()
	  canvas.addGLEventListener(view);
          getContentPane().add(canvas);
 
          this.addWindowListener(this);  
          canvas.addMouseListener(this);
          canvas.addMouseMotionListener(this);
          canvas.addKeyListener(this);
  }

  class SceneView implements GLEventListener
  {
    ...My 3d stuff is here..perfect
    private int mx:
    private int my:

    public void setMX( int x )
    {
      mx = x;
    }

    public void setMY( int y )
    {
      my = y;
    }

    public void init(GLAutoDrawable arg0) 
    {
    }
    
    public void display(GLAutoDrawable arg0) 
    {
        ...
        // now mx and my are regular members of this class
        float doSomeStuff= mx*0.5+my*0.3+bar;
     }
  }

  @Override
  public void mouseMoved(MouseEvent arg0) {
	// just store the values in the outer class
	int mx=arg0.getX();
	int my=arg0.getY();
        view.setMX(mx);
        view.setMY(my);
	mouse_x = mx;
	
	System.out.println("Maus X"+mx+" Y"+my);
   }
}