From 4f359fa1a77d73eec35e8f924688318f4d9e6351 Mon Sep 17 00:00:00 2001 From: daniel watson Date: Sat, 17 Sep 2016 21:37:45 -0700 Subject: [PATCH] minimal example to show an image --- ShowImage.java | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 ShowImage.java diff --git a/ShowImage.java b/ShowImage.java new file mode 100644 index 0000000..91c3072 --- /dev/null +++ b/ShowImage.java @@ -0,0 +1,59 @@ + +import java.awt.Graphics; +import java.awt.Panel; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.net.URL; + +import javax.imageio.ImageIO; +import javax.swing.JFrame; + +public class ShowImage extends Panel { + BufferedImage image; + + public ShowImage() { + try { + // image file + // name is + // test.png + URL input = this.getClass().getResource(("test.png")); + // this will read the image to save + // into a BufferedImage variable + image = ImageIO.read(input); + } catch (IOException ie) { + // this is to spill + // out a message + // saying + // something + // doesn't work + System.out.println("Error: " + ie.getMessage()); + } + } + + public void paint(Graphics g) { + // This is to actually draw the image out, + // necessary. + + // Input of a bufferedimage, x, y, then + // an observer in this case null. + g.drawImage(image, 0, 0, null); + } + + static public void main(String args[]) throws Exception { + // Creates a window to + // display the image + JFrame frame = new JFrame("Display image"); + + // Gets the image from ShowImage() method + Panel panel = new ShowImage(); + + // Adds the image to the new window + // panel + frame.getContentPane().add(panel); + + // sets the size of the panel + frame.setSize(500, 500); + // makes it visible + frame.setVisible(true); + } +} -- 2.30.2