Recipe 11.16. Viewing an Image from a URL with Swing and JythonCredit: Joel Lawhead, Chuck Parker ProblemYou want to make a simple Swing image viewer, accepting the URL to an image and displaying the image in a Swing window. SolutionJython 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") DiscussionSwing'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 AlsoSwing docs are at http://java.sun.com/docs/books/tutorial/uiswing/; Jython is at http://www.jython.org. |