cranebird
YaBB Newbies
Offline
Posts: 2
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); } ----