Using mouse in jPCT applet version

Started by Tornado7, May 14, 2004, 12:02:03 PM

Previous topic - Next topic

Tornado7

Hi,

I'm experimenting on using mouse in my jPCT applet following, as trace, your way to map keys in your Bounce applet example; so I've initialized a variable:

private boolean click=false;

I've definited the following method:

public boolean mouseClick(Event e, int key) {
       ProcessKey(key,true);
       return (true);
}

in ProcessKey I've added the following "case":

case (MouseEvent.MOUSE_CLICKED):     {wire=event; break;}


where 'wire' is the the wireframe mode, here just as example. At last, I've added the following 'if' in the timer():

if (wire) {
                        wireframe=!wireframe;
               }

but, running the applet, nothing happens (using the 1 key, for example, the wireframe mode works, so the problem is just on the mouse event).
I've to add a mouse listener..... or something else?

Bye and thanks
:)

EgonOlsen

What is mouseClick()? I suggest to overwrite

protected void processMouseEvent(MouseEvent e)

in the applet with something like:


protected void processMouseEvent(MouseEvent e) {
  super.processMouseEvent(e);
  if (e.getID()==MouseEvent.MOUSE_CLICKED) {
     wire=!wire;
  }
}


That should work. You shouldn't abuse processKey() for mouse events, because it was meant to handle events from the keyboard, not from the mouse.

Tornado7

I've tried the this example following your code:


public void processMouseEvent(MouseEvent e) {
    super.processMouseEvent(e);
    if (e.getID()==MouseEvent.MOUSE_CLICKED) {
        System.out.println("I'm pressing the Mouse Click Button");
    }
 }


but, looking in Sun Java Console, nothing happens.... I'm becoming crazy
:?

EgonOlsen

I see...the method doesn't get called, because the component (the applet in this case) doesn't get mouse events by default. You may either implement your own mouse listener or, if you want to use the method above, enable this type of event in your applet by calling
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
somewhere in the beginning.

Tornado7

I've tried both solutions: I've added  this.enableEvents(AWTEvent.MOUSE_EVENT_MASK); in init method, and I've tried the other solution:

I've implemented the MouseListener interface:

public class ThreeDSimApplet extends Applet implements Runnable, MouseListener

and I've added the required methods:

public void mouseClicked(MouseEvent event) {
   if (event.getButton()==MouseEvent.BUTTON1) {
        mouseX=event.getX();
            mouseY=event.getY();
       
            System.out.println(mouseX+" ," + mouseY);
   }
}
 
public void mousePressed(MouseEvent event) {
}
 
public void mouseReleased(MouseEvent event) {
}
 
public void mouseEntered(MouseEvent event) {
}

public void mouseExited(MouseEvent event) {
}    

in both solution the mouse click now works but...... the keyboard events are disabled...... Is it possible to catch both events?

EgonOlsen

Have you implemented a KeyListener together with the MouseListener?

Tornado7

No, I haven't. I've implemented this morning, and now they works both. Thanks for your suggestions  :D