We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Processing with Jython, add EventListener patch
Page Index Toggle Pages: 1
Processing with Jython, add EventListener patch (Read 1594 times)
Processing with Jython, add EventListener patch
Sep 7th, 2008, 3:28pm
 
Hello Guys,

I hope to develop my sketch interactivelly and tried jython(http://www.jython.org/Project/).
It's very easy to use jython with proce55ing, but I found it's not so interactive environment.
When I modified my subclass of PApplet, I should close running PApplet and launch it again because I have no way to re-define class.

I got the idea to make PApplet.draw() method event-driven. My idea is to define new Event(PDrawEvent) and treat PApplet as an EventListener. By this small patch and jython, I could modify code without closing PApplet window.

I hope you'll be interested in this patch.


Session jython with Pached PApplet sample:
('>>>' is jython prompt. create PApplet instance, and add modify it's behaviour without close window.)

>>> from processing.core import PApplet
>>> p=PApplet()
>>> from java.awt import Frame
>>> f=Frame(size=(200,200))
>>> f.add(p)
processing.core.PApplet[panel1,0,0,0x0,invalid,layout=java.awt.FlowLayout]
>>> f.visible = 1
>>> def fn(evt):
...
p.line(0, 0, p.random(p.width), p.random(p.height))
...
>>> p.doDraw = fn
>>> p.init()
>>> p.stop()
>>> def gn(evt):
...
p.line(p.random(p.width), p.random(p.height), p.random(p.width), p.random(p.height))
...
>>> p.doDraw = gn
>>> p.init()

my patch to PApplet:

--- PApplet.java
2008-09-07 21:41:11.000000000 +0900
+++ PApplet.java.new
2008-09-07 21:41:56.000000000 +0900
@@ -156,7 +156,7 @@
 */
public class PApplet extends Applet
  implements PConstants, Runnable,
-             MouseListener, MouseMotionListener, KeyListener, FocusListener
+             MouseListener, MouseMotionListener, KeyListener, FocusListener,Serializable
{
  /**
   * Full name of the Java version (i.e. 1.5.0_11).
@@ -527,6 +527,28 @@
  // for 0116, the CRUSTY_THREADS are being disabled to fix lots of bugs.
  static final boolean CRUSTY_THREADS = false; //true;

+  // to avoid the tight coupling between PApplet class and draw() method,
+  // add PDrawEvent/PDrawEventListener.
+  private PDrawEventListener pdrawEventListener = null;
+  
+  // uni-cast style event listener.
+  public void addPDrawEventListener(PDrawEventListener listener) throws TooManyListenersException {
+    if (pdrawEventListener != null) {
+      throw new TooManyListenersException("PDrawEventListener is uni cast.");
+    }
+    pdrawEventListener = listener;
+  }
+  
+  public void removePDrawEventListener(PDrawEventListener listener) {
+    pdrawEventListener = null;
+  }
+
+  public void firePDrawEventListener(PDrawEvent evt) {
+    // if no EventListener registered, do nothing.
+    if (pdrawEventListener != null) {
+      pdrawEventListener.doDraw(evt);
+    }
+  }

  public void init() {
//    println("Calling init()");
@@ -888,7 +910,9 @@
  public void draw() {
    // if no draw method, then shut things down
    //System.out.println("no draw method, goodbye");
-    finished = true;
+    // fire PDrawEvent
+    firePDrawEventListener(new PDrawEvent(this));
+    //    finished = true;
  }


PDrawEvent.java (new class):
----
package processing.core;

import java.util.EventObject;
import java.io.Serializable;

public class PDrawEvent extends EventObject implements Serializable{
   public PDrawEvent (Object source) {

super(source);
   }
}
----
PDrawEventListener.java (new class):
----
package processing.core;

import java.util.EventListener;

public interface PDrawEventListener extends EventListener {
   public void doDraw(PDrawEvent evt);
}
----
Re: Processing with Jython, add EventListener patc
Reply #1 - Sep 8th, 2008, 4:11am
 
just use redraw(), this is already built in.
Re: Processing with Jython, add EventListener patc
Reply #2 - Sep 8th, 2008, 5:27pm
 
fry wrote on Sep 8th, 2008, 4:11am:
just use redraw(), this is already built in.


Thank you for your reply, but I feel in sense of embarrassment.

I'm sorry for my misleading statement,  but I don't want to redraw. It seems that redraw method just redraw..
I want to replace draw method in run time without destroy PApplet instance.  
I think it is impossible to replace draw method with original PApplet, because draw method is bind to the class tightly. I think it is possible only if I create new instance.

(I'm sorry I'm not good at English ..)
Re: Processing with Jython, add EventListener patc
Reply #3 - Dec 9th, 2008, 8:59pm
 
I hope too, but I have problem with running Jython. I've "installed" Processing version with java. I put java.exe in my PATH and jython.jar in my CLASSPATH, but I can't start Jython from command line just typing c:\> java -jar jython.jar , but when I'm in the jython directory this command works well. What's going wrong?
Page Index Toggle Pages: 1