Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Load Image

Posted on 2005-03-04
11
Medium Priority
?
326 Views
Last Modified: 2010-03-31

    Dear programmers,
    I want to create an image icon by using Java.  Here is my code:

    ImageIcon initImage = new ImageIcon("image.gif");
   
    setBackground(Color.white);
    setBorder(BorderFactory.createLineBorder(Color.black));
   
    content = new JLabel (initImage);
   
    add(content);


   Why didn't the picture in image.gif show up?  I created image.gif by Photoshop (in Transparent mode).  In addition, this image.gif is in the same folder as the classes that I am working on.
   What should I do?
0
Comment
Question by:gocong
  • 6
  • 4
10 Comments
 

Author Comment

by:gocong
ID: 13457374
I would appreciate any helps.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13457385
Try:

ImageIcon initImage = new ImageIcon( this.getClass().getResource( "image.gif" ) );
0
 

Author Comment

by:gocong
ID: 13457410

   I tried it, it was ok when I compiled it.  However, it didn't show up the picture in image.gif
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:TimYates
ID: 13457469
Can you post your actual code?

If you do:

    content = new JLabel (initImage);
    content.setText( "Label" ) ;

does the text show up?  (just checking you are actually seeing the label) ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13457473
Ok, if your class is called:

    org.stuff.YourClass

and "image.gif" is in org/stuff

can you try:

    ImageIcon initImage = new ImageIcon( YourClass.class.getResource( "/org/stuff/image.gif" ) );

ie:  if the class is called YourClass (not in a package), and image.gif is in the same dir, can you try:

    ImageIcon initImage = new ImageIcon( YourClass.class.getResource( "/image.gif" ) );
0
 

Author Comment

by:gocong
ID: 13457532
  I added "content.setText("Label") into my code and "Label" showed up every time I clicked mouse.

  Here is my code:

  public class Cell extends JPanel{
 
  public static final int One = 1;
  public static final int Two = 2;
 
  private static final String  IMAGE_FILE_1 = "image1.gif";
  private static final String  IMAGE_FILE_2 ="image2.gif";
  private static final String  IMAGE_FILE_3 = "image3.gif";
 
  private JLabel content;
  private Point location;
 
  public  Cell(){
    this(null);
  }  
 
  public  Cell(Point pt){
//    ImageIcon initImage = new ImageIcon("image1.gif");
    ImageIcon initImage = new ImageIcon( this.getClass().getResource( "image1.gif" ) );
    setBackground(Color.white);
    setBorder(BorderFactory.createLineBorder(Color.black));
   
    content = new JLabel (initImage);
     
    add(content);
   
    location = pt;
  }  
 
  public Point getPosition(){
    return location;
  }  
 
  public void setContent(int image){
    switch (image){
      case One: content.setIcon(new ImageIcon(IMAGE_FILE_1));
                 break;
      case Two:  content.setIcon(new ImageIcon(IMAGE_FILE_2));
            break;
      default:              // do nothing
        break;
    }
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13457551
is Cell in a package?

if not, try:

----------

  public class Cell extends JPanel{
 
  public static final int One = 1;
  public static final int Two = 2;
 
  private static final ImageIcon image1 = new ImageIcon( Cell.class.getResource( "/image1.gif" ) ) ;
  private static final ImageIcon image2 = new ImageIcon( Cell.class.getResource( "/image2.gif" ) ) ;
  private static final ImageIcon image3 = new ImageIcon( Cell.class.getResource( "/image3.gif" ) ) ;
 
  private JLabel content;
  private Point location;
 
  public  Cell(){
    this(null);
  }  
 
  public  Cell(Point pt){
    setBackground(Color.white);
    setBorder(BorderFactory.createLineBorder(Color.black));
   
    content = new JLabel ( image1 );
     
    add(content);
   
    location = pt;
  }  
 
  public Point getPosition(){
    return location;
  }  
 
  public void setContent(int image){
    switch (image){
      case One: content.setIcon( image2 );
                 break;
      case Two:  content.setIcon( image3 );
            break;
      default:              // do nothing
        break;
    }
  }
}
0
 

Author Comment

by:gocong
ID: 13457651
 Compilation is successful, but the image didn't show up.  Cell is not in package.  Sorry, I am so tired.  I am going to sleep now.  I will chat with you later.  Thanks for your help.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 700 total points
ID: 13457883
Sleep well!   (while I ramble on and on) ;-)

Ok, can you post the contents of the directory containing the class?

And the command you use to execute the program?

And your classpath (if you have it set)?

Assuming you jave something like:

Is any exception thrown when the program runs?

This is odd...

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13504113
Can I ask why the grade C?

Grade C means you couldn't be bothered to delete this question :-(    (http://www.experts-exchange.com/help.jsp#hi73)

Some sort of explanation why my comments were rubbish would have been nice...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question