title: Java Image Panel File Import
body: <p>I am currently having an issue with my code that it won't import an image from the file dialog. Basically it's meant to import the image when selected and place it on an Image Panel which is implemented.
  Here is the code I have written for the File Import</p>
  
  <pre><code>FileDialog dialog = new FileDialog((Frame)null, "Select File to Open");
  dialog.setMode(FileDialog.LOAD);
  dialog.setVisible(true);
  String file = dialog.getFile();
  imageP.readImage(file);
  imageP.repaint();
  </code></pre>
  
  <p>Here is the Image Panel Class</p>
  
  <pre><code>class ImagePanel extends JPanel
  {
      int numCols, numRows;
      BufferedImage image = null;
      private final int xOffset = 0;
      private final int yOffset = 0;
  
      public ImagePanel()
      {
          readImage("");
      }
  
      public void readImage(String filename)
      {
          try {
              image = ImageIO.read(new File(filename));
              numRows = image.getHeight();
              numCols = image.getWidth();
          }
          catch( IOException e )
          {
              e.printStackTrace();
          }
      }
  
      public void paintComponent( Graphics g )
      {
          Graphics2D g2 = (Graphics2D)g;
          g2.clearRect(0, 0, this.getWidth(), this.getHeight());
          if (image != null)
          {
              g2.drawImage( image, xOffset, yOffset, null );
          }
      }
  }
  </code></pre>
  
  <p>I currently get an IIOException which says can't read file at the moment.
  If anyone can help me with this issue that would be great.</p>