Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

How can I add a buffered image to my jframe

I am new to Java and am trying to learn how to add objects to my window.  My window has a menu bar with an Open option on it.  When Open is clicked, I use Java's FileChooser to allow the user to choose an image to display.  I have the code working so the image is read into a BufferedImage.  What I haven't been able to figure out is how to display the image in my window.  Here is what I have tried but the image doesn't display in the window.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;



public class ImageEditor extends JFrame implements ActionListener
{
   public static BufferedImage image = null;
     
   public ImageEditor()
   {
      super("ImageEditor");
      
   }
   
   public BufferedImage readImage(String fileName) throws IOException
   {
      File file = new File(fileName);
      return ImageIO.read(file);
   }

   public void actionPerformed(ActionEvent e) 
   {
      System.out.println(e.getActionCommand());
      switch(e.getActionCommand())
      {
         case "Open":
            JFileChooser chooser = new JFileChooser();
            //FileFilter filter = new FileNameExtensionFilter(
             //  "JPG & GIF Images", "jpg", "gif");
            //chooser.setFileFilter(filter);
            chooser.setMultiSelectionEnabled(false);
            int returnVal = chooser.showOpenDialog(this);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
               System.out.println("You chose to open this file: " +
                  chooser.getSelectedFile().getPath());
               try
               {
                  image = readImage(chooser.getSelectedFile().getPath().toString());
                  System.out.println(image.getWidth());
                  System.out.println(image.getHeight());
               }
               catch(IOException x)
               {
                  
               }
               JLabel jLabel = new JLabel(new ImageIcon(image));
               JPanel jPanel = new JPanel();
               jPanel.add(jLabel);
               this.add(jPanel);
               super.repaint();
            }
            break;
            
         case "Exit":
            System.exit(0);
            break;
      }
      
   }

   public JMenuBar createMenuBar() 
   {
      JMenuBar menuBar;
      JMenu menu,menu2;
      JMenuItem menuItem,menuItem2;
   
        //Create the menu bar.
      menuBar = new JMenuBar();
   
        //Build the first menu.
      menu = new JMenu("File");
      menu.setMnemonic(KeyEvent.VK_A);
      menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
      menuBar.add(menu);
      
      menu2 = new JMenu("Image");
      menu2.setMnemonic(KeyEvent.VK_A);
      menu2.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
      menuBar.add(menu2);
   
   
        //a group of JMenuItems
      menuItem = new JMenuItem("Open", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Increase box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu.add(menuItem);
   
      menuItem = new JMenuItem("Save", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu.add(menuItem);
      
      menuItem = new JMenuItem("Exit", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu.add(menuItem);
      
      menuItem = new JMenuItem("makeRed", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeGreen", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeBlue", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeColorsInverted", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeGreyScale", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("makeWarhol", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("flipHorizontal", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("flipVertical", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      menuItem = new JMenuItem("rotateClockwise", KeyEvent.VK_T);
      menuItem.getAccessibleContext().setAccessibleDescription("Decrease box size by 10%");
   	// Add an action handler to this menu item
      menuItem.addActionListener( this );
      menu2.add(menuItem);
      
      return menuBar;
   }
   
   public static void main(String[] args)
   {
      ImageEditor window = new ImageEditor();
      window.setJMenuBar(window.createMenuBar());
      window.setBounds(100, 100, 800, 600);
      window.setDefaultCloseOperation(EXIT_ON_CLOSE);
      window.setVisible(true);
   }
}

Open in new window


Any help is greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh
dyarosh

ASKER

Thank you
Did it work? Because I notice you do call paint(), and that when your gui is resized, then the image shows.
Just FYI, what I came up with was :

Image i = image.getScaledInstance(this.getWidth(),this.getHeight(),Image.SCALE_SMOOTH);
              JLabel jLabel = new JLabel(new ImageIcon(i));

               JPanel jPanel = new JPanel();
               jPanel.add(jLabel);
	this.add(jPanel);
               	this.pack();

Open in new window

Avatar of dyarosh

ASKER

Thanks I will see if that works.