私は、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
これらの2つのエラーは非常に似ているようです。私はここで間違って何をしていますか? java.awtは私が解析しているクラスと互換性がありませんか?これをどうすれば解決できますか?
ご迷惑をおかけして申し訳ありません。
こんにちは、 私は実際にプログラムを動作させる解決策を見つけましたが、問題に対処しません。私が使用していた "core.jar"ファイルはProcessing 3.2.3からのものでした。 Processing 1.5.1と2.2.1の "core.jar"で変更しましたが、どちらもエラーがなくなり、プログラムが機能しました。 –