我正在开发一个程序,它接受用户的输入,并生成一个输出作为地图投影图。我找到的最简单的地图投影库是matplotlib-basemap,它是用Python语言编写的,而我并不太熟悉(我在Java上工作).I用Java语言编写的用户界面。目前,我正在执行python代码,并使用运行时和调用".py“文件的exec()命令发送带有数据的命令数组。这将执行该命令,并将绘图显示为单独的窗口。
我的问题是:有没有可能在Jpanel上嵌入这个底图(与缩放功能交互)?或者在python GUI上,然后可以嵌入到JPanel上?我知道我可以将matplotlib生成的图像保存为一个可以在面板上修复的文件,但这样它就不能交互了,缩放功能也就不再可用了。或者使用基于Java的工具比使用底图更合适?(我还没有找到这样好的工具)
-2013年5月22日编辑
Jython不是一个解决方案,因为matplotlib与它不兼容。我同意用python做所有的事情都是最好的,但这就是我必须要做的。
雅各布·贾尔:我找不到一个示例代码来展示如何在JPanel或JFrame上嵌入独立的应用程序(底图)。
目前,我正计划将底图嵌入到wxpython GUI中,然后使用套接字在两种语言之间进行通信。
带有服务器Java和客户端Python的TCP/IP套接字。
发布于 2013-05-14 16:21:29
这是如果你对新想法和学习新事物持开放态度。
这不是解决你的特定问题的解决方案,你想要连接两种语言,这是一个替代想法,将所有东西都整合到Python中。
#!/usr/bin/python
import pyglet
from time import time, sleep
class Window(pyglet.window.Window):
def __init__(self):
super(Window, self).__init__(vsync = False)
self.alive = 1
self.click = None
self.drag = False
with open('map.png', 'rb') as fh:
self.geodata_image = pyglet.image.load('map.png', file=fh)
self.geo_info = self.geodata_image.width, self.geodata_image.height
def on_draw(self):
self.render()
def on_mouse_press(self, x, y, button, modifiers):
self.click = x,y
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.click:
self.drag = True
print 'Drag offset:',(dx,dy)
def on_mouse_release(self, x, y, button, modifiers):
if not self.drag and self.click:
print 'You clicked here', self.click, 'Relese point:',(x,y)
## Do work on corindate
else:
print 'You draged from', self.click, 'to:',(x,y)
## Move or link two points, or w/e you want to do.
self.click = None
self.drag = False
def render(self):
self.clear()
## An alternative to only getting a region if you only want to show a specific part of
## the image:
# subimage = self.geodata_image.get_region(0, 0, self.geo_info[0], self.geo_info[1])
self.geodata_image.blit(0, 0, 0) # x, y, z from the bottom left corner
self.flip()
def on_close(self):
self.alive = 0
def run(self):
while self.alive:
self.render()
## self.dispatch_events() must be in the main loop
## or any loop that you want to "render" something
## Because it is what lets Pyglet continue with the next frame.
event = self.dispatch_events()
sleep(1.0/25) # 25FPS limit
win = Window()
win.run()你所需要的是:
作为Java子模块运行的Python模型
import sys
for line in sys.stdin.readline():
if line == 'plot':
pass # create image here例如。
发布于 2013-05-15 21:02:09
您当然可以将您的图形用户界面嵌入到Jython中,这是Python实现。不幸的是,它不支持matplotlib,因为它依赖于本机代码。您可以尝试使用execnet从Jython中调用Python。
https://stackoverflow.com/questions/16142184
复制相似问题