为什么我在 Eclipse 中尝试使用 Jython 进行 运行 处理时不断出现类似的错误?

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

我一直在尝试使用 Jython 运行 处理几个(java 基础)草图。但是,在这两种情况下,我都会遇到无法解决的类似错误。 这是我的第一段非常基本的代码:

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())

我收到以下错误:

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

我正在尝试 运行 的另一段代码是这样的:

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

这次我得到的错误是:

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

这两个错误看起来很相似。我在这里做错了什么? java.awt 与我要解析的 class 不兼容吗?我该如何解决这个问题?

非常感谢任何帮助。

我的猜测是,这是 PApplet 不再扩展 Applet 并且不再直接嵌入到处理 3 中的 JFrame 的结果。有关详细信息,请参阅this page.

大概您正在使用的库依赖于 PApplet 扩展 Applet,因此它不适用于 Processing 3。这就是为什么您返回旧版本 Processing 的解决方案有效.

你必须检查你的库的文档,看看是否有办法让它与 Processing 3 一起工作。否则你现在只能使用旧版本的 Processing。