Skip to content

Instantly share code, notes, and snippets.

@ssorgatem
Created March 22, 2012 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssorgatem/2157922 to your computer and use it in GitHub Desktop.
Save ssorgatem/2157922 to your computer and use it in GitHub Desktop.
An example of integrating the Jmol viewer into a jython application, with optional console.
#-*- coding:utf-8 -*-
#
# Copyright 2012 Adrià Cereto Massagué <adrian.cereto@urv.cat>
# This is the jython version of Jmol's Integration.java.
# It is under public domain
"""
An example of integrating the Jmol viewer into a jython application, with optional console.
"""
from java.lang.System import exit
from java.awt import BorderLayout , Container, Dimension, Graphics
from java.awt.event import WindowAdapter
from javax.swing import JFrame, JPanel
from org.jmol.adapter.smarter import SmarterJmolAdapter
from org.jmol.api import JmolViewer
from org.jmol.util import Logger
from org.openscience.jmol.app.jmolpanel import AppConsole
#strXyzHOH = """3
#water
#O 0.0 0.0 0.0
#H 0.76923955 -0.59357141 0.0
#H -0.76923955 -0.59357141 0.0
#"""
strScript = "delay; move 360 0 0 0 0 0 0 0 4;"
def main():
"""
Demonstrates a simple way to include an optional console along with the applet.
"""
frame = JFrame("Hello", size = (410, 700))
frame.addWindowListener(ApplicationCloser())
contentPane = frame.getContentPane()
jmolPanel = JmolPanel(preferredSize = Dimension(400, 400))
# main panel -- Jmol panel on top
panel = JPanel(layout = BorderLayout())
panel.add(jmolPanel)
# main panel -- console panel on bottom
panel2 = JPanel(layout = BorderLayout(), preferredSize = Dimension(400, 300))
console = AppConsole(jmolPanel.viewer, panel2,"History State Clear")
# You can use a different JmolStatusListener or JmolCallbackListener interface
# if you want to, but AppConsole itself should take care of any console-related callbacks
jmolPanel.viewer.jmolCallbackListener = console
panel.add("South", panel2)
contentPane.add(panel)
frame.visible = True
# sample start-up script
#viewer.openStringInline(strXyzHOH)
strError = jmolPanel.viewer.openFile("http://chemapps.stolaf.edu/jmol/docs/examples-11/data/caffeine.xyz")
if strError == None:
jmolPanel.viewer.evalString(strScript)
else:
Logger.error(strError)
class ApplicationCloser(WindowAdapter):
def windowClosing(self, e):
exit(0)
class JmolPanel(JPanel):
def __init__(self, preferredSize = None):
self.preferredSize = preferredSize
self.currentSize = Dimension()
self.viewer = JmolViewer.allocateViewer(self, SmarterJmolAdapter(), None, None, None, None, None)
def paint(self, g):
self.getSize(self.currentSize)
self.viewer.renderScreenImage(g, self.currentSize.width, self.currentSize.height)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment