Converting an Image to a BufferedImage in Swing

This following example reads a JPEG imgage from disk, converts it to a bufferedimage and displays it onto the screen.

import java.awt.event.*;
import java.awt.image.*;
import java.awt.color.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame
   public Main() throws Exception {
      BufferedImage bi = createBufferedImage("ally.jpg");
      // add the image inside a JLabel component to the window
      getContentPane().add(new JLabel(new ImageIcon(bi)));
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
   public BufferedImage createBufferedImage(String filename) {
      Image img = Toolkit.getDefaultToolkit().getImage(filename);
      try {
         MediaTracker tracker = new MediaTracker(this);
         tracker.addImage(img, 0);
      catch ( Exception e ) {} 
      BufferedImage bi = 
         new BufferedImage(img.getWidth(this), img.getHeight(this),
      Graphics2D g2d = bi.createGraphics();
      g2d.drawImage(img, 0, 0, this);
      return bi;
   public static void main(String args[]) throws Exception {
      Main main = new Main();;