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

edited December 2016 in Python Mode

Hi there dear enthusiasts,

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.

Answers

  • Hi there,

    I actually found a solution which makes the program work, but it does not address the problem. The "core.jar" file which I was using was from Processing 3.2.3. I changed it with the "core.jar" from Processing 1.5.1 and 2.2.1 and for both of them I no longer got the error and the program worked.

  • this

    frame.add(panel)
    

    is trying to add a PApplet to a frame. if PApplet isn't one of Component or Popup menu then you'll have trouble. and PApplet changed sometime in P3, away from extending Applet

    processing-3.1.1/core/library/core.jar
    public class PApplet implements PConstants

    processing-3.0b7/core/library/core.jar
    public class PApplet implements PConstants

    processing-2.2.1/core/library/core.jar
    public class PApplet extends Applet implements PConstants, Runnable, MouseListener, MouseWheelListener, MouseMotionListener, KeyListener, FocusListener {...

  • and PApplet changed sometime in P3

    I've repeated that zillion times already: :-<
    Latest version which still extends Java's Applet class is 3.0a5! :(|)

  • Though I would still recommend using version 2.2.1

  • Thanks guys!!!! Also I was wondering why my mouse functions like "mousePressed" are not working. I changed the first code a little bit to make the colour of the rectangle dependent on the press of the mouse:

    from processing.core import *
    from processing.event import *
    from processing.data import *
    from processing.opengl import *
    
    
    
    class HelloProcessing(PApplet):
    
    
        def setup(self):
    
            global p
            p = self
            p.size(350, 350)
    
        def draw(self):
            if p.mousePressed == True:
                p.fill(255)
            else:
                p.fill(0)
            p.rect(150, 150, 50, 50)
    
    
    
    
    
    
    
    if __name__ == '__main__':
        import pawt
        pawt.test(HelloProcessing())
    

    But it does not seem to work. any ideas why not?

  • I have no idea what that pawt is for.
    But AFAIK, class PApplet needs its methods main() or runSketch() in order to "ignite" it! :-?

  • The mouse does not work in my other longer code which is not using pawt. Would you please point me to an example or let me know how I can use "mousePressed' and other Processing mouse functions? Also I have not used main() or runSketch(), but Processing functions such as fill() and rect() seem to work, but mouse stuff does not work. Any ideas why?

  • edited December 2016

    Sorry, I barely know anything about Python or its "ecosystem". X_X
    That's just my Java Mode knowledge.
    What its pre-processor does to transpile a ".pde" to an actual ".java" file. ~O)

  • I am not really sure about the pre-processor either. I really want to make this code work on eclipse so then I can integrate it with other python code and run it on a server as a GUI ( I am referring to the longer piece of code). Nonetheless, if you find anything or any guides relevant to my case, I would really appreciate it, if you could let me know. I really want to get the mouse working

  • In order to use Processing code on Eclipse or other IDE's, it's important to know how PDE's pre-processor works. That's all I know. 8-|

  • I see. I actually switched "mousePressed" with "mouseButton" and specified the button and it works! For some reason "mousePressed" is not working. Moreover, when I use print(mousePressed) I get something weird like <bound method Sketch.mousePressed of org.python.proxies.__main__$Sketch$0[panel0,0,0,300x300,layout=java.awt.FlowLayout]>

    But when I do the same with "mouseButton" I actually get an integer value. It seems that some functions are not working? I am not exactly sure what is going on.

  • edited December 2016

    In Java, fields, methods & classes, each got its own namespace.
    It means we can use the same label name for each of those 3 categories at the same time!
    However, we don't have separate namespaces for JS. And I believe that's true for Python as well. :-@

  • Hi ! I have the same problem, did you fix it ? I need help too !

Sign In or Register to comment.