Results 1 to 17 of 17

Thread: Display image

  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Display image

    Hi,

    I have made a program to put and get information from a database.
    In one of my fields I put a filename for a picture and I would like to display this picture in my program.
    How can I do this?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Display image

    What is your program? is it a desktop program or is it a web application? please give some more details on your problem.

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Display image

    It is a desktop program.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Display image

    And where are you stuck?
    Is the image coming from the database or somewhere else?
    What code do you have?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Display image

    The picture is in a folder. One of the fields in the database contains the path to the picture. I would like to have the picture displayed when I select a row in the database.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Display image

    OK.
    So where have you got to?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Display image

    I know how to get data from a row in the database and display Strings or Int into textboxes (JTextField). The question is what kind of Swing container or control to use for displaying pictures, and the display command.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Display image

    Use a JLabel, with the Icon constructor.
    The ImageIcon class will be the one you'll pass into it, which has a constructor that takes a file path.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Display image

    Have you ever been pointed to the Tutorial Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials) ? You could have found this information, along with other stuff you'll need to know, in the pages of that trail.

    In the course of implementing this, if you have a Swing question please be sure to post it in the AWT/Swing section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Display image

    Thanks for your help. The tutorials are fine, but sometimes I don't find exactly what I look for. :)

    I have tested some code and think I getting closer... Heres the testcode that works:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    public class LoadImageApp extends Component {
               
        
    	
    	BufferedImage img;
     
        public void paint(Graphics g) {
            g.drawImage(img, 0, 0, null);
        }
     
        public LoadImageApp() {
           try {
               img = ImageIO.read(new File("c:/temp/test.jpg"));
           } catch (IOException e) {
           }
     
        }
     
        public Dimension getPreferredSize() {
            if (img == null) {
                 return new Dimension(100,100);
            } else {
               return new Dimension(img.getWidth(null), img.getHeight(null));
           }
        }
     
        public static void main(String[] args) {
     
            JFrame f = new JFrame("Testbilde");
                 
            f.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
     
            f.add(new LoadImageApp());
            f.pack();
            f.setVisible(true);
        }
    }
    Now I am trying to get it into a small program written in Netbeans. Her I have a textfield that I would like to write the path for the picture and then display the picture in a panel.
    But I am not sure how to do this.
    Here is the code so far:
    Java Code:
    package GUI;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    
    public class Image extends javax.swing.JFrame {
    
        BufferedImage img = null;
    
        {
            initComponents();
        }
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            textPicture = new javax.swing.JTextField();
            panelPicture = new javax.swing.JPanel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            textPicture.setToolTipText("");
            textPicture.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyReleased(java.awt.event.KeyEvent evt) {
                    textPictureKeyReleased(evt);
                }
            });
    
            javax.swing.GroupLayout panelPictureLayout = new javax.swing.GroupLayout(panelPicture);
            panelPicture.setLayout(panelPictureLayout);
            panelPictureLayout.setHorizontalGroup(
                panelPictureLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE)
            );
            panelPictureLayout.setVerticalGroup(
                panelPictureLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 245, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(panelPicture, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(textPicture, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(textPicture, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(panelPicture, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        public Image() {
               try {
               img = ImageIO.read(new File("c:/temp/test.jpg"));
           } catch (IOException e) {
           }
        }
        
        public void paint(Graphics g) {
            g.drawImage(img, 0, 0, null);
    }
        
        public Dimension getPreferredSize() {
            if (img == null) {
                 return new Dimension(500,500);
            } else {
               return new Dimension(img.getWidth(null), img.getHeight(null));
           }
        }
        
        private void textPictureKeyReleased(java.awt.event.KeyEvent evt) {
            if(evt.getKeyChar() == KeyEvent.VK_ENTER){
                
                // what to write here....
            }
        }
         
        public static void main(String args[]) {
            
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Image.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Image().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JPanel panelPicture;
        private javax.swing.JTextField textPicture;
        // End of variables declaration
    }
    I have written a comment in there "What to write here"
    The Enter event should display the picture written in the textfield.
    Any suggestions what to write here?

    If I get this to work, the last step is to get the picture from the database...

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

    Default Re: Display image

    Quote Originally Posted by asai View Post
    The tutorials are fine, but sometimes I don't find exactly what I look for. :)
    Tutorials are to be gone through, systematically. There's no shortcut to knowledge.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Display image

    I totally agree, but another type of gaining knowledge is asking questions when you are stuck.
    In this case I get the picture displayed when my programs only task is to display the image.
    However now I want it to display the picture when I ask it to.
    So, I need a little hint...

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Display image

    Do not use the GUI builder.
    That's point 1.
    It is not a tool for people who don't understand how Swing works.

    You need to write this by hand, especially since it's quite simple.

    And I would (again) suggest using an ImageIcon given to a JLabel for displaying your image.
    It's a lot easier than faffing about with painting if all you want is the image to appear.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Display image

    Quote Originally Posted by asai View Post
    I totally agree, but another type of gaining knowledge is asking questions when you are stuck.
    To be answered by those who did take the time and effort to learn from the tutorials?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Display image

    Thanks Tolls... ImageIcon and JLabel. Works perfectly now...

  16. #16
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Display image

    Another question related to this. (The question is only where to find the tutorial, because I don't know what to search for...)
    I would like to convert this caracter:
    Java Code:
    \
    to this caracter:
    Java Code:
    /
    Again, I am only interested in what this is called and where to look for the tutorial for it.

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

    Default Re: Display image

    Read the API for java.lang.String.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Display Image (png)
    By vimalaranjan in forum Java Gaming
    Replies: 2
    Last Post: 05-04-2011, 12:23 AM
  2. to display image
    By asmitarnd in forum AWT / Swing
    Replies: 6
    Last Post: 04-07-2011, 11:17 AM
  3. image display
    By asmitarnd in forum AWT / Swing
    Replies: 3
    Last Post: 04-07-2011, 11:17 AM
  4. How to display image ?
    By Birkoff in forum AWT / Swing
    Replies: 7
    Last Post: 06-09-2008, 07:58 AM

Posting Permissions

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