Solved

How can I display image in eclipse

Posted on 2008-06-16
16
560 Views
Last Modified: 2013-12-15
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.
Image image = Image.createImage("/MyImage.png");

Open in new window

0
Comment
Question by:GLUZMANS
  • 7
  • 5
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21792158
Depends on how you want to do it. Normally best as


BufferedImage bi = javax.imageio.ImageIO.read(getClass().getResource("/MyImage.png"));
0
 
LVL 92

Expert Comment

by:objects
ID: 21792392
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

0
 

Author Comment

by:GLUZMANS
ID: 21793379
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.

 

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21793477
Please post your code. Something like

getResource("/res/MyImage.png")

should be used

0
 

Author Comment

by:GLUZMANS
ID: 21796412
The code is very simply. I only want to draw an image into the canvas displayable.

thanks.

image = Image.createImage("/MyImage.png");

.

.

.

protected void paint(Graphics g){

   g.drawImage(image, 20, 20 , 0);

}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21796542
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);

}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21796555
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);

}

Open in new window

0
 

Author Comment

by:GLUZMANS
ID: 21796896
thanks, but as I wrote before my question is about J2ME programming. javax is not included in the J2ME platform.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:GLUZMANS
ID: 21797850
I mean javax.imageio.ImageIO ofcourse (-:
0
 
LVL 92

Expert Comment

by:objects
ID: 21798414
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)

0
 

Author Comment

by:GLUZMANS
ID: 21798559
It is not working.

*  I tried to put the image.png file into the root,src and  deploy folders.
0
 
LVL 92

Expert Comment

by:objects
ID: 21798587
are any of those source folders?

0
 
LVL 92

Expert Comment

by:objects
ID: 21798595
What does Class.getResourceAsStream("/MyImage.png") return?

0
 

Author Comment

by:GLUZMANS
ID: 21798747
The return result of :  getClass().getResourceAsStream("/MyImage.png"   is:
com.sun.midp.io.ResourceInputStream@1cb37664
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 21798755
may be having problems reading that image, try a different image

0
 

Author Closing Comment

by:GLUZMANS
ID: 31467535
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 76
Eclipse IDE - Cannot copy/paste from console output 8 126
Cipher Configuration on Apache HTTPD 4 50
servlet filter example 37 37
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now