2016-12-08 49 views
0

我一直在嘗試使用Jython運行幾個處理(基於Java的)草圖。但是,在這兩種情況下,我都遇到了類似的錯誤,我無法解決。 這是我的第一塊非常基本的代碼:爲什麼在嘗試在Eclipse中使用Jython運行處理時不斷收到類似的錯誤?

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是否與我正在解析的類不兼容?我怎樣才能解決這個問題?

任何幫助,非常感謝。

+1

你好, 我實際上找到了一個解決方案,使程序工作,但它沒有解決問題。我使用的「core.jar」文件來自Processing 3.2.3。我使用Processing 1.5.1和2.2.1中的「core.jar」對它進行了更改,對於它們兩個,我都不再出現錯誤並且程序正常工作。 –

回答

0

我的猜測是,這是PApplet不再延長Applet,不再直接嵌入在JFrame作爲加工3.欲瞭解更多信息,請參閱this page結果。

假設您使用的庫依賴於PApplet擴展Applet,所以它不會與Processing 3一起使用。這就是爲什麼您的解決方案返回到較舊版本的Processing。

你必須檢查你的庫的文檔,看看是否有辦法使它與Processing 3一起工作。否則,你現在被卡住使用舊版本的Processing。

相關問題