我正在开发一个程序,该程序接受用户输入并将输出生成为 map 投影图。 我找到的最简单的 map 投影库是 matplotlib-basemap,它是用 python 编写的,这是一种我不太熟悉的语言(我在 Java 上工作)。我用 Java 编写了用户界面。目前我正在执行 python 代码并使用运行时和调用“.py”文件的 exec() 命令发送带有数据的命令数组。 这将执行命令并将绘图显示为单独的窗口。
我的问题是: 是否可以在 Jpanel 上嵌入此 basemap (与缩放功能交互)?或者在可以嵌入到 JPanel 上的 python GUI 上? 我知道我可以将 matplotlib 生成的图像保存为一个可以固定在面板上的文件,但它不会是交互式的,缩放功能将不可用。 还是使用基于 Java 的工具而不是 basemap 更合适?(我还没有找到更好的工具)
----2013年5月22日编辑------
Jython 不是解决方案,因为 matplotlib 与其不兼容。在 python 中完成所有事情我同意是最佳的,但这是我必须处理的。
JACOB Jar:我找不到展示如何在 JPanel 或 JFrame 上嵌入单独的应用程序( basemap )的示例代码。
目前我正计划将 basemap 嵌入到 wxpython GUI 中,然后使用套接字在两种语言之间进行通信。
带有服务器 Java 和客户端 Python 的 TCP/IP 套接字。
最佳答案
如果您对新想法和学习新事物持开放态度。
这不是您想要加入两种语言的特定问题的解决方案,这是对将所有内容合并到 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()
您只需要:
作为 Javav 子模块运行的 Python 模型
import sys
for line in sys.stdin.readline():
if line == 'plot':
pass # create image here
例如。
关于java - 来自 Java 的 Python GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16142184/