Author Topic: Using mouse in jPCT applet version  (Read 6464 times)

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Using mouse in jPCT applet version
« on: May 14, 2004, 12:02:03 pm »
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
 :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Using mouse in jPCT applet version
« Reply #1 on: May 14, 2004, 04:07:08 pm »
What is mouseClick()? I suggest to overwrite

Code: [Select]
protected void processMouseEvent(MouseEvent e)

in the applet with something like:

Code: [Select]

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.

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Using mouse in jPCT applet version
« Reply #2 on: May 17, 2004, 11:46:42 am »
I’ve tried the this example following your code:

Code: [Select]

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
 :?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Using mouse in jPCT applet version
« Reply #3 on: May 17, 2004, 04:56:46 pm »
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
 
Code: [Select]
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
somewhere in the beginning.

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Using mouse in jPCT applet version
« Reply #4 on: May 18, 2004, 12:02:04 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Using mouse in jPCT applet version
« Reply #5 on: May 19, 2004, 07:26:21 am »
Have you implemented a KeyListener together with the MouseListener?

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Using mouse in jPCT applet version
« Reply #6 on: May 19, 2004, 11:15:01 am »
No, I haven’t. I’ve implemented this morning, and now they works both. Thanks for your suggestions  :D