Recipe 11.16. Viewing an Image from a URL with Swing and Jython
Credit: Joel Lawhead, Chuck Parker
Problem
You want to make a simple Swing
image viewer, accepting the URL to an image and displaying the image
in a Swing window.
Solution
Jython makes this task very easy:
from pawt import swing
from java import net
def view(url):
frame = swing.JFrame("Image: " + url, visible=1)
frame.getContentPane( ).add(swing.JLabel(swing.ImageIcon(net.URL(url))))
frame.setSize(400,250)
frame.show( )
if _ _name_ _ == '_ _main_ _':
view("http://www.python.org/pics/pythonHi.gif")
Discussion
Swing's JLabel and
ImageIcon widgets can be easily combined in Jython
to make a simple image viewer. The need to pass a URL to the
view function is not at all a limitation, because
you can always use the file: protocol in your URL
if you want to display an image that lives on your filesystem rather
than out there on the Web. Remember that the U in URL stands for
Universal!
See Also
Swing docs are at http://java.sun.com/docs/books/tutorial/uiswing/;
Jython is at http://www.jython.org.