Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Loading and Displaying a user chosen image

  1. #1
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Loading and Displaying a user chosen image

    Java Code:
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    import java.io.*;
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import javax.imageio.ImageIO;
    import java.awt.*;
    
    public class MenuOfImage extends JFrame
    {
       
      public MenuOfImage()
      {
        initUI();
      }
      
      public final void initUI()
      {
        JMenuBar jmb=new JMenuBar();
        JMenu jm=new JMenu("FILE");
        jm.setMnemonic(KeyEvent.VK_F);
        JMenuItem jmi=new JMenuItem("OPEN");
        JMenuItem jmi1=new JMenuItem("EXIT");
        jmi1.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae)
          {
            System.exit(0);
          }
      });
        jmi.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae)
         {
          JFileChooser fileopen = new JFileChooser();
          FileFilter filter = new FileNameExtensionFilter("gif files", "gif");
          fileopen.addChoosableFileFilter(filter);
    
          int ret = fileopen.showDialog(null, "Open file");
    
          if (ret == JFileChooser.APPROVE_OPTION) {
            File file = fileopen.getSelectedFile();
            String spath=file.getAbsolutePath();
            try
            {
            BufferedImage image = ImageIO.read(new File(spath));
            LoadandShow las=new LoadandShow(image);
            }
            catch(Exception e){}
        }
    
         }
        });
        
      jm.add(jmi);
      jm.add(jmi1);
      jmb.add(jm);
      setJMenuBar(jmb);
      setTitle("SKIN TONE STEGANOGRAPHY");
      setSize(600,600);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
      public static void main(String args[])
      {
        SwingUtilities.invokeLater(new Runnable(){
          public void run()
          {
            MenuOfImage moi=new MenuOfImage();
            moi.setVisible(true);
          }
        });
      }
    }
    class LoadandShow
    {
      BufferedImage image;
      public LoadandShow(BufferedImage image)
      {
        this.image = image;
      }
       protected void paintComponent(Graphics g) 
       {
            g.drawImage(image,300,300,this);
        }
    }
    I am doing a project on image processing.Here I am trying to load a gif image from the system.I am getting a few errors.Kindly Help me.
    Thanks in advance

  2. #2
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Loading and Displaying a user chosen image

    Sorry I forgot to mention the error.The error is in paintComponent().The prototype suggests I pass an object of type ImageObserver.How do I do that?
    Thank You again

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Loading and Displaying a user chosen image

    Does the code compile?

    If not and you can't understand the compiler's messages, post them and indicate which lines of your code they are referring to.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Loading and Displaying a user chosen image

    Our posts crossed.

    You can always use null as the image observer argument instead of "this" as you have it now. (this makes sense if you not using an image observer for anything.)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Loading and Displaying a user chosen image

    Even if you got that code to compile, it wouldn't do anything since
    1. the image isn't being added into any displayable component
    2. a paintComponent method won't do anything by itself unless it is a legitimate override method of class that derives (ultimately) from JComponent.
    3. You're probably better off simply loading the image into an ImageIcon and the icon into a JLabel and then displaying the JLabel.

  6. #6
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Loading and Displaying a user chosen image

    I read somewhere that if I use ImageIcon I will not be able to manipulate the image.Kindly tell me what to do?
    Thank You

  7. #7
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Loading and Displaying a user chosen image

    I have managed to load and display the image using ImageIcon but the image appears only after I minimize and redisplay the window.

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.image.BufferedImage;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    import java.io.*;
    import javax.swing.JFileChooser;
    import javax.swing.filechooser.FileFilter;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import javax.imageio.ImageIO;
    import java.awt.*;
    
    
    public class MenuOfImage extends JFrame
    {
       
      public MenuOfImage()
      {
        initUI();
      }
      
      public void initUI()
      {
        
        JMenuBar jmb=new JMenuBar();
        JMenu jm=new JMenu("FILE");
        jm.setMnemonic(KeyEvent.VK_F);
        JMenuItem jmi=new JMenuItem("OPEN");
        JMenuItem jmi1=new JMenuItem("EXIT");
        jmi1.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent ae)
          {
            System.exit(0);
          }
      });
        jmi.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae)
         {
          JFileChooser fileopen = new JFileChooser();
          FileFilter filter = new FileNameExtensionFilter("gif files", "gif");
          fileopen.addChoosableFileFilter(filter);
    
          int ret = fileopen.showDialog(null, "Open file");
    
          if (ret == JFileChooser.APPROVE_OPTION) 
          {
            JPanel panel=new JPanel();
            File file = fileopen.getSelectedFile();
            String spath=file.getAbsolutePath();
            ImageIcon imgicon=new ImageIcon(spath);
            JLabel label=new JLabel(); 
            label.setIcon(imgicon);
            panel.add(label);
            getContentPane().add(panel);
          }
    
         }
        });
        
      jm.add(jmi);
      jm.add(jmi1);
      jmb.add(jm);
      setJMenuBar(jmb);
      setTitle("SKIN TONE STEGANOGRAPHY");
      setIconImage(Toolkit.getDefaultToolkit().getImage("cartoon.gif"));
      setSize(600,600);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
      public static void main(String args[])
      {
        SwingUtilities.invokeLater(new Runnable(){
          public void run()
          {
            MenuOfImage moi=new MenuOfImage();
            moi.setVisible(true);
          }
        });
      }
    }
    Please help me with it.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default Re: Loading and Displaying a user chosen image

    You're adding a Component (the JLabel and JPanel) to a Container that's already shown. Whenever you do that (or remove a component) you need to revalidate() and repaint() the container.
    Java Code:
    :
    :
              panel.add(label);
              getContentPane().add(panel);
              ((JComponent) getContentPane()).revalidate();
              getContentPane().repaint();
            }
    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    forwardbias is offline Member
    Join Date
    Mar 2012
    Posts
    23
    Rep Power
    0

    Default Re: Loading and Displaying a user chosen image

    Thank you so far for the help.I have a few more questions....
    1)How do I resize the image before placing it in the Panel and Where do I insert the coding for it?Can I do it using Dimension?
    2)My project is on steganography.Will I face problems later on now that I have used a label to display the image?If yes how do I incoorporate the change using BufferedImage?
    3)I am new to this forum so please guide me towards a few EASY TO UNDERSTAND swing books(preferred) or tutorials in this site or from where ever u awesomely intellectual people study.I ll be really grateful.
    Thank YOU Very much

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Loading and Displaying a user chosen image

    2)My project is on steganography.Will I face problems later on now that I have used a label to display the image?If yes how do I incoorporate the change using BufferedImage?
    I think you may have mentioned this before, but it wasn't directly addressed.

    I'm no expert, but this is what I would try... In the load actionPerformed() you create an image icon as suggested. I would actually have a BufferedImage image instance variable and would give it a value as you did in your original code. Then I would create the image icon from this image (there is a constructor that does that), and create the label using the icon and add it (as you are doing).

    The point of this extra step is that you then have an image variable visible throughout the class. You can do steganographically clever things to this image, just so long as whenever you change it you also tell the label to repaint itself.

    -----

    As far as changing the size of the image goes there is a lesson on this in Oracle's Tutorial: Advanced Topics in Java 2D. It covers all sorts of transformations including enlargement. And also controlling the rendering quality which can be involved when you alter an image's size.

    The 2D section of the Tutorial is something I would read from start to finish - or at least anything concerned with images - before embarking on image processing. If nothing else it tells you the sorts of things that the standard API lets you do easily. One thing to beware of is that steganography will (probably) rely on the value of the least significant bits of pixel colour. You will need to be careful that any resizing or other processing doesn't destroy the information that is being hidden in these bits.

Similar Threads

  1. Displaying a square based on user's input
    By luke in forum New To Java
    Replies: 15
    Last Post: 07-14-2012, 09:43 AM
  2. Loading a image with Image and ImageIcon
    By cel0x in forum AWT / Swing
    Replies: 3
    Last Post: 02-15-2012, 10:38 AM
  3. help with displaying calculation with user input
    By bamagirl31 in forum New To Java
    Replies: 18
    Last Post: 05-26-2011, 04:00 AM
  4. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  5. how do i count the letter that the user had chosen?
    By ashin in forum Advanced Java
    Replies: 4
    Last Post: 10-08-2009, 05:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •