Results 1 to 4 of 4
  1. #1
    kevbo423 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Displaying an image in Java

    Sorry if this is a re-thread.
    I'm trying to display an image in java for the first time, and I'm completely stuck. I found this program on the internet but I can't get it to work. Thanks for the help.

    import java.io.*;
    import java.awt.*;
    import javax.swing.*;

    public class Main extends JFrame{
    public Main(){
    setSize(500,500);
    JPanel panel = new JPanel();
    panel.setBackground(Color.CYAN);
    ImageIcon icon = new ImageIcon("C:\Users\Tate\Desktop\test1PIC.jpg");
    JLabel label = new JLabel();
    label.setIcon(icon);
    panel.add(label);
    this.getContentPane().add(panel);

    setVisible(true);
    }
    public static void main (String[] args){
    new Main();
    }
    }

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

    Default Re: Displaying an image in Java

    I'm surprised that that code compiled at all. Have you tried swapping your back slashes "\" for forward slashes "/"? And are you sure that your image file path is correct?

  3. #3
    kevbo423 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Displaying an image in Java

    I actually figured it out. I didn't name the file the same as the class.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Displaying an image in Java

    Nothing advanced about that. Moving to New to Java.

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

Similar Threads

  1. Image not displaying
    By sandeep43 in forum New To Java
    Replies: 20
    Last Post: 07-08-2011, 12:31 AM
  2. Problem with displaying an image in Java Swing
    By BackInSoftware in forum Advanced Java
    Replies: 10
    Last Post: 01-11-2011, 06:56 PM
  3. Displaying an image
    By BariMutation in forum New To Java
    Replies: 6
    Last Post: 11-12-2010, 01:23 AM
  4. Displaying an image
    By SWEngineer in forum AWT / Swing
    Replies: 11
    Last Post: 01-28-2010, 05:48 AM
  5. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 06:01 AM

Tags for this Thread

Posting Permissions

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