Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an easy way to display an Image in Java/Scala?

I want to play around with some image manipulation code I'm writing, so I'd like to use the Scala console or something like BlueJ's Java CodePad to create a java.awt.Image and then just pop it up to look at.

Ideally, it'd just be a panel or something without a Frame that would appear and I could just click it to make it disappear. Is there a way to make something that lightweight, or do I really need some kind of frame and more scaffolding?

like image 851
Todd O'Bryan Avatar asked Jan 26 '12 23:01

Todd O'Bryan


2 Answers

JOptionPane.showMessageDialog(parent, new JLabel(new ImageIcon(theImage)));

Of course, if you have an URL for the image (or can form one from a File path), it can also be displayed as a tool-tip using HTML.

See Show full Image when hover over thumbnail for source.

like image 142
Andrew Thompson Avatar answered Nov 07 '22 17:11

Andrew Thompson


In Scala, use the Java Swing ImageIcon

val img = new ImageIcon("path/to/file.jpg")

with Scala Swing Dialog

Dialog.showMessage(message = img)

Or better, display the image instead of the alert icon:

Dialog.showMessage(message = null, icon = img)
like image 39
Luigi Plinge Avatar answered Nov 07 '22 18:11

Luigi Plinge