Since the GTimer class is not dependant on any of the other clases in the G4P library then I have a fairly straightforward solution.
In your sketch -
- create a tab called GTimer.java
- copy all the source code from the GTimer class into this tab (delete the package statement as it is no longer needed)
- modify the createEventHandler method in the copied class to accept parameters. (see below)
- modify the constructore to accept parameters (see below)
- protected void createEventHandler(Object obj, String methodName, Class[] parameters){
- try{
- this.eventHandler = obj.getClass().getMethod(methodName, parameters );
- eventHandlerObject = obj;
- } catch (Exception e) {
- eventHandlerObject = null;
- System.out.println("The class " + obj.getClass().getSimpleName() + " does not have a method called " + methodName);
- }
- }
Now modify the constructor to accept parameters
- public GTimer(PApplet theApplet, Object obj, String methodName, Class[] parameters, int delay){
- app = theApplet;
- createEventHandler(obj, methodName, parameters);
- // If we have something to handle the event then create the Timer
- if(eventHandlerObject != null){
- timer = new Timer(delay, new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- fireEvent();
- }
- });
- timer.setInitialDelay(delay);
- timer.setDelay(delay);
- timer.stop();
- }
- }
The changes needed are highlighted so to register a method called doThis that has a String and an Integer class as parameters then
- GTimer timer = new GTimer(this, this, "doThis", new Class[] {String.class, Integer.class}, 50);
The sketch handler method
- void doThis(String s, Integer i){
- ...
- }
If you are using the G4P library for other controls as well I suggest you rename the copied class and its tab to something different.