?
Solved

Load Image

Posted on 2005-03-04
11
Medium Priority
?
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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