//import java.util.*; import javax.swing.*; import java.awt.*; //import java.awt.event.*; /** * ImageTest.java * * @author Jeff Ondich * date: 2013.10.28 * * Simple demo showing how to display an image file (jpeg/gif/png/etc.) */ public class ImageTest extends JFrame { public ImageTest() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(500, 300); this.setTitle("Look! A moose!"); this.setVisible(true); } public void paint(Graphics g) { String mooseFilePath = System.getProperty("user.dir") + "/moose.jpeg"; System.out.println(mooseFilePath); Image mooseImage = Toolkit.getDefaultToolkit().getImage(mooseFilePath); Graphics2D g2 = (Graphics2D)g; g2.drawImage(mooseImage, 100, 100, 400, 200, 0, 0, mooseImage.getWidth(null), mooseImage.getHeight(null), null); } public static void main(String[] args) { ImageTest imageTest = new ImageTest(); // For some reason, an immediate call to repaint doesn't show my // moose. But if I introduce a short delay, something in the initialization // of JFrame must finish, meaning that my repaint actually produces an // image. I'm still researching why this happens, but if you know the answer, // I'd love to be set straight. try { Thread.sleep(50); } catch (Exception e) { } imageTest.repaint(); } }