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!
Java

Avatar of undefined
Last Comment
dyarosh

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
krakatoa

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dyarosh

ASKER
Thank you
krakatoa

Did it work? Because I notice you do call paint(), and that when your gui is resized, then the image shows.
krakatoa

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
dyarosh

ASKER
Thanks I will see if that works.