Link to home
Start Free TrialLog in
Avatar of gocong
gocong

asked on

Load Image


    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?
Avatar of gocong
gocong

ASKER

I would appreciate any helps.
Avatar of TimYates
Try:

ImageIcon initImage = new ImageIcon( this.getClass().getResource( "image.gif" ) );
Avatar of gocong

ASKER


   I tried it, it was ok when I compiled it.  However, it didn't show up the picture in image.gif
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) ;-)
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" ) );
Avatar of gocong

ASKER

  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;
    }
  }
}
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;
    }
  }
}
Avatar of gocong

ASKER

 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.
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can I ask why the grade C?

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

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