// From A Beginning Programmer's Guide to Java
// at http://beginwithjava.blogspot.com/

// Used in the article 'Loading and Displaying and Image in Java'
// at http://beginwithjava.blogspot.com/2009/01/loading-and-displaying-image-in-java.html
// uses the image Duke_Blocks.gif
// from https://duke.dev.java.net/images/web/Duke_Blocks.gif
// You can substitute your own image file. It needs to be in the same directory as
// this program.
// For more information, see the original article.
// Mark Graybill, Sept 2008

/* ShowImage.java

  This program loads and displays an image from a file.

  mag-13May2008
  updated 20Feb2009 by mag to incorporate suggestions
    by mazing and iofthestorm on digg.
*/

// Import the basic graphics classes.
import java.awt.*;
import javax.swing.*;

public class ShowImage extends JPanel{
   Image image; // Declare a name for our Image object.

  // Create a constructor method
  public ShowImage(){
      super();
      // Load an image file into our Image object. This file has to be in the same
      // directory as ShowImage.class.
      image = Toolkit.getDefaultToolkit().getImage("Duke_Blocks.gif");
  }

  // The following methods are instance methods.

  /*    Create a paintComponent() method to override the one in
      JPanel.
      This is where the drawing happens.
      We don't have to call it in our program, it gets called
      automatically whenever the panel needs to be redrawn,
      like when it it made visible or moved or whatever.
  */
  public void paintComponent(Graphics g){

      // Draw our Image object.
      g.drawImage(image,50,10,200,200, this); // at location 50,10
                                              // 200 wide and high
  }

  public static void main(String arg[]){
      JFrame frame = new JFrame("ShowImage");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(600,400);

      ShowImage panel = new ShowImage();
      frame.setContentPane(panel); 
      frame.setVisible(true); 
  }
}