1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Java: Trying to display an image

Discussion in 'Software Development' started by DYS, Jan 20, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. DYS

    DYS Thread Starter

    Joined:
    Dec 28, 2008
    Messages:
    462
    Hi guys.

    Today is the first time I've tried to import and display an image in a Java file I am creating, so I thought I'd try a test before I work on my proper file.

    Anyway, I created this short file, which should display an image, and some text below. I have followed a simple example from a text book, exactly the way they have written it, but the image does not show - the text works fine.

    I am just trying to import an image of Tom Cruise giving a thumbs-up for a laugh.

    Code:
    import java.awt.*;
     import java.awt.event.*;
     import javax.swing.*;
    
    
    public class JImageTest extends JFrame
    {
        private JLabel tomCruiseImage;
        
        private JLabel text;
    
        // no-argument constructor
        public JImageTest()
        {
            createUserInterface();
        }
        
        public void createUserInterface()
        {
            Container contentPane = getContentPane();
            contentPane.setLayout(null);
              
            // JLabel which should display the image      
            tomCruiseImage = new JLabel();
            tomCruiseImage.setIcon(new ImageIcon("tc_tu.jpeg"));
            tomCruiseImage.setBounds(0, 0, 339, 450);
            tomCruiseImage.setHorizontalAlignment(JLabel.CENTER);
            contentPane.add(tomCruiseImage);        
            
            // Text - works fine!
            text = new JLabel();
            text.setText("Well done! It works!");
            text.setBounds(20, 500, 760, 100);
            text.setFont(new Font("Arial", Font.BOLD, 36));
            contentPane.add(text);
        
            // Creates the window and titles it
            setTitle("Here is... Tom Cruise");
            setSize(800, 600);
            setVisible(true);    
        }
        
        public static void main (String[] args)
        {
            JImageTest application = new JImageTest();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
    }
    This may not be the best way to do it, but I can understand this way, and I have followed this sort of layout in my other file that I am working on. So if anyone can give any suggestions as to why this is not working, then I'd appreciate it.

    Please note that the image file, tc_tu.jpeg, is in the same directory as the Java file and the class file.

    Thanks in advance :)
     
  2. DaBeers

    DaBeers

    Joined:
    Oct 30, 2008
    Messages:
    276
    I think the problem is that you’re trying to set a jpeg as an icon. An icon is an image file type of its own (.ico) and is usually made up of several images, or more precisely, the same image in different scales, packaged together.

    If what you are looking to do is display images, check the code on this page:
    http://www.roseindia.net/java/example/java/swing/DisplayImage.shtml

    It uses the paint method, which gives you control over how the image is drawn, but also may take some reading up on to properly use.

    GL,
     
  3. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    You can use a jpeg as an icon for a jlabel.
    Try specifying an absolute path for the image, it may be that where you have put the image isn't where java is looking for it.

    Andy
     
  4. DYS

    DYS Thread Starter

    Joined:
    Dec 28, 2008
    Messages:
    462
    Okay, thanks guys, I'll give it another go.

    I think I tried putting the full path of the image, andythepandy, but I will have another go as I have not touched this for a few weeks.
     
  5. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    Sorry for jumping into this thread and asking a different question. But, DaBeers and andy, do you have any good java resources like this?

    Sorry again for jumping in to this thread.
     
  6. DYS

    DYS Thread Starter

    Joined:
    Dec 28, 2008
    Messages:
    462
    Seems like all I had to do was use a PNG file. It now works! Thanks guys for getting me to double check this.

    sepala, I have no problem with you "jumping" this thread. I hope you can get it sorted.
     
  7. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    Thanks :)
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/975786

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice