Solved: Java: Trying to display an image

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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 :)
 
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,
 
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
 

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.
 
Joined
May 20, 2010
Messages
4,040
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,
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.
 

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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top