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.cr
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?
Try:
ImageIcon initImage = new ImageIcon( this.getClass().getResourc e( "image.gif" ) );
ImageIcon initImage = new ImageIcon( this.getClass().getResourc
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) ;-)
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.getResourc e( "/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.getResourc e( "/image.gif" ) );
org.stuff.YourClass
and "image.gif" is in org/stuff
can you try:
ImageIcon initImage = new ImageIcon( YourClass.class.getResourc
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.getResourc
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().getResourc e( "image1.gif" ) );
setBackground(Color.white) ;
setBorder(BorderFactory.cr eateLineBo rder(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;
}
}
}
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().getResourc
setBackground(Color.white)
setBorder(BorderFactory.cr
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.cr eateLineBo rder(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;
}
}
}
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.cr
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;
}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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...
ASKER