| 1 | |
| 2 | import java.awt.Graphics; |
| 3 | import java.awt.Panel; |
| 4 | import java.awt.image.BufferedImage; |
| 5 | import java.io.IOException; |
| 6 | import java.net.URL; |
| 7 | |
| 8 | import javax.imageio.ImageIO; |
| 9 | import javax.swing.JFrame; |
| 10 | |
| 11 | public class ShowImage extends Panel { |
| 12 | BufferedImage image; |
| 13 | |
| 14 | public ShowImage() { |
| 15 | try { |
| 16 | // image file |
| 17 | // name is |
| 18 | // test.png |
| 19 | URL input = this.getClass().getResource(("imgs/test.png")); |
| 20 | // this will read the image to save |
| 21 | // into a BufferedImage variable |
| 22 | image = ImageIO.read(input); |
| 23 | } catch (IOException ie) { |
| 24 | // this is to spill |
| 25 | // out a message |
| 26 | // saying |
| 27 | // something |
| 28 | // doesn't work |
| 29 | System.out.println("Error: " + ie.getMessage()); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public void paint(Graphics g) { |
| 34 | // This is to actually draw the image out, |
| 35 | // necessary. |
| 36 | |
| 37 | // Input of a bufferedimage, x, y, then |
| 38 | // an observer in this case null. |
| 39 | g.drawImage(image, 0, 0, null); |
| 40 | } |
| 41 | |
| 42 | static public void main(String args[]) throws Exception { |
| 43 | // Creates a window to |
| 44 | // display the image |
| 45 | JFrame frame = new JFrame("Display image"); |
| 46 | |
| 47 | // Gets the image from ShowImage() method |
| 48 | Panel panel = new ShowImage(); |
| 49 | |
| 50 | // Adds the image to the new window |
| 51 | // panel |
| 52 | frame.getContentPane().add(panel); |
| 53 | |
| 54 | // sets the size of the panel |
| 55 | frame.setSize(500, 500); |
| 56 | // makes it visible |
| 57 | frame.setVisible(true); |
| 58 | } |
| 59 | } |