Search code examples
javapythonprocessingjython

Why do I keep getting similar errors while trying to run processing using Jython in Eclipse?


I have been trying to run a couple of processing (java based) sketches using Jython. However, in both cases I am getting similar errors which I can not resolve. Here is my first piece of very basic code:

from processing.core import PApplet

class HelloProcessing(PApplet):
def setup(self):
    global p
    p = self
    p.size(350, 350)

def draw(self):
    p.fill(p.random(255))
    p.rect(150, 150, 50, 50)

if __name__ == '__main__':
    import pawt
    pawt.test(HelloProcessing())

I get the following errors:

Traceback (most recent call last):
  File "/home/nimanamjouyan/workspace/LearningPyDev/src/helloProcessing.py", line 15, in <module>
    pawt.test(HelloProcessing())
  File "/home/nimanamjouyan/jython-installer-2.7.0/Lib/pawt/__init__.py", line 9, in test
    f.add('Center', panel)
TypeError: add(): 1st arg can't be coerced to String, java.awt.Component

The other piece of code I am trying to run is this:

from javax.swing import JFrame
from processing.core import PApplet
from random import uniform

winWidth=600
winHeight=500
numBoxes = 1000
boxes = []

class RandBoxes (PApplet):

    def __init__(self):
        pass
    def setup(self):
        self.size(winWidth, winHeight, self.JAVA2D)
        while len(boxes)<numBoxes:
            boxes.append(Box(self,     uniform(0,winWidth),uniform(0,winHeight)))
        print "number of boxes = %d" % len(boxes)
    # rqd due to PApplet's using frameRate and frameRate(n) etc.
    def getField(self, name):
        #return self.class.superclass.getDeclaredField(name).get(self)
        return self.PApplet.getDeclaredField(name).get(self)

    def draw(self):
        self.background(128)
        for b in boxes:
            b.step()
        if self.frameCount % 10 == 0:
            print "frameRate=%f frameCount=%i" % (self.getField('frameRate'), self.frameCount)

class Box(object):
    def __init__(self, p, x, y):
        self.p = p
        self.x = x
        self.y = y
        self.c = p.color(255,255,0)
    def step(self):
        self.x = max(min(self.x+uniform(-1,1),winWidth),0)
        self.y = max(min(self.y+uniform(-1,1),winHeight),0)
        self.paint()
    def paint(self):
        self.p.noStroke()
        self.p.fill(self.c)
        self.p.rect(self.x-2,self.y-2,5,5)

if __name__ == '__main__':
    frame = JFrame(title="Processing",resizable =  0,defaultCloseOperation=JFrame.EXIT_ON_CLOSE)
    panel = RandBoxes()
    frame.add(panel)
    panel.init()
    while panel.defaultSize and not panel.finished:
    pass
    frame.pack()
    frame.visible = 1

The error I am getting this time is:

Traceback (most recent call last):
  File "/home/nimanamjouyan/workspace/LearningPyDev/src/RandBoxesTest.py", line 54, in <module>
    frame.add(panel)
TypeError: add(): 1st arg can't be coerced to java.awt.PopupMenu, java.awt.Component

These two errors seem to be very similar. What am I doing wrong here? is java.awt incompatible with the class that I am parsing to? How can I fix this?

Any help is much appreciated.


Solution

  • My guess is that this is a result of PApplet no longer extending Applet and no longer being directly embedded in a JFrame as of Processing 3. For more info, see this page.

    Presumably the library you're using relies on PApplet extending Applet, so it won't work with Processing 3. That's why your solution of going back to an older version of Processing works.

    You'd have to check your library's documentation to see if there's a way to make it work with Processing 3. Otherwise you're stuck using the older versions of Processing for now.