How to use mouseClicked in processing - eclipse

Halli Hallo,

I am looking to make a simple library in eclipse - which contains a button - whereby the Button has a "MouseEvent" actionListener. The snippet of code looks like this

public void mouseClicked(MouseEvent e){
        if(mouseOver()){
            //Do something
    }

    public void mouseReleased(MouseEvent e){
        msClicked = false;
    }

    public void mouseEvent(MouseEvent event) {
        switch (event.getAction()) {
/*      case MouseEvent.PRESS:
            mousePressed(event);
            break;*/
        case MouseEvent.RELEASE:
            mouseReleased(event);
            break;
        case MouseEvent.CLICK:
            mouseClicked(event);
            break;
/*      case MouseEvent.DRAG:
            mouseDragged(event);
            break;
        case MouseEvent.MOVE:
            mouseMoved(event);
            break;
        case MouseEvent.ENTER:
            mouseEntered(event);
            break;
        case MouseEvent.EXIT:
            mouseExited(event);
            break;*/
        }
    }

At line 3 I can define what to do. But I want to make it so that the User defines what the click does in processing after calling the library, - and not edit the library itself. I know mousePressed - (bool) works fine, but I am a bit stuck with mouseClicked.

In processing I always did it so

void mouseClicked(){
    if(mouseOverButton()){
        doSomething();
    }
}

Is it possible to do what I want to, without using Processing's "void mouseClicked()"?

Tagged:

Answers

  • AFAIK, you have to use the registerMethod() method from PApplet to get your method to be called by Processing.

  • edited May 2014

    yes that is true and I have done so. I don't think my question is clear enough, or I ahve misunderstood you. I can use

    public void mouseClicked(MouseEvent e){
    if(mouseOver()){
    //Do something
    }
    

    just fine, but this is in the library, not in the processing code. This would mean that for the user to make a button do what the user wants, he has to edit the library to act on mouseClicked() - which is not practical.

  • Answer ✓

    Create an interface defining the method to be called upon the event. Make your library to accept any object implementing this interface and calling the method upon the event.

    Thus, the user creates a class (anonymous or not) and instantiates an object of that class, where the method does what she wants. And pass this object to the library.

Sign In or Register to comment.