Created
January 27, 2013 15:22
-
-
Save MarkyC/4648825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package name.marcocirillo; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import javax.imageio.ImageIO; | |
| import javax.swing.ImageIcon; | |
| import javax.swing.JFrame; | |
| import javax.swing.JLabel; | |
| public class TestImageResouce { | |
| public static ImageIcon createIcon(String path) throws IOException { | |
| ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
| InputStream input = classLoader.getResourceAsStream(path); | |
| return new ImageIcon(ImageIO.read(input)); | |
| } | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| createAndShowGUI(); | |
| } | |
| private static void createAndShowGUI() { | |
| JFrame f = new JFrame(); | |
| try { | |
| f.add(new JLabel(createIcon("img/smiley-face.jpg"))); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| System.exit(1); | |
| } | |
| f.pack(); | |
| f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| f.setVisible(true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment