GLUZMANS
asked on
How can I display image in eclipse
hi,
I am trying to display image in java, but the image isn't display at all.
My questions are:
What is the problem?Where I should put the image file and how can I deploy the source file with the image file?
thanks.
I am trying to display image in java, but the image isn't display at all.
My questions are:
What is the problem?Where I should put the image file and how can I deploy the source file with the image file?
thanks.
Image image = Image.createImage("/MyImage.png");
to display an image from a file see here
http://www.objects.com.au/java/qa/926375938.html
Using that method the image can be where you want it
Easit way to display an image is using a JLabel, see here for an example
http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html
When loading it as a resource as in this example the image needs to be located in your classpath
http://www.objects.com.au/java/qa/926375938.html
Using that method the image can be where you want it
Easit way to display an image is using a JLabel, see here for an example
http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html
When loading it as a resource as in this example the image needs to be located in your classpath
ASKER
My apologized. The question should be in j2me zone.
I inserted the image file into res directory under my project.
And I still getting an exception: java.io.IOException.
I inserted the image file into res directory under my project.
And I still getting an exception: java.io.IOException.
Please post your code. Something like
getResource("/res/MyImage. png")
should be used
getResource("/res/MyImage.
should be used
ASKER
The code is very simply. I only want to draw an image into the canvas displayable.
thanks.
thanks.
image = Image.createImage("/MyImage.png");
.
.
.
protected void paint(Graphics g){
g.drawImage(image, 20, 20 , 0);
}
OK, so make that
image = javax.imageio.ImageIO.read(getClass().getResource("/MyImage.png"));
...
public void paint(Graphics g){ // possibly paintComponent if JPanel
g.drawImage(image, 20, 20 , 0);
}
OK, so make that
image = javax.imageio.ImageIO.read(getClass().getResource("/res/MyImage.png"));
...
public void paint(Graphics g){ // possibly paintComponent if JPanel
g.drawImage(image, 20, 20 , 0);
}
ASKER
thanks, but as I wrote before my question is about J2ME programming. javax is not included in the J2ME platform.
ASKER
I mean javax.imageio.ImageIO ofcourse (-:
try putting the image file i the root directory of your jar
and make sure you load it after the midlet has completed initialising (after init() has been called)
and make sure you load it after the midlet has completed initialising (after init() has been called)
ASKER
It is not working.
* I tried to put the image.png file into the root,src and deploy folders.
* I tried to put the image.png file into the root,src and deploy folders.
are any of those source folders?
What does Class.getResourceAsStream( "/MyImage. png") return?
ASKER
The return result of : getClass().getResourceAsSt ream("/MyI mage.png" is:
com.sun.midp.io.ResourceIn putStream@ 1cb37664
com.sun.midp.io.ResourceIn
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Its working! I changed the image and it is working.
It is important to emphasize for others , that the code for creates an immutable image was still : image = Image.createImage ("/MyImage.png") . I put the images only in the "res" folder,
and in the "Properties for res => Native Library => Location path" , I inserted the "res" location path, that is "MyProject/res".
thanks a lot objects.
It is important to emphasize for others , that the code for creates an immutable image was still : image = Image.createImage ("/MyImage.png") . I put the images only in the "res" folder,
and in the "Properties for res => Native Library => Location path" , I inserted the "res" location path, that is "MyProject/res".
thanks a lot objects.
BufferedImage bi = javax.imageio.ImageIO.read