Why there are nothing after I used drawImage()?

Hello guys, I got a problem in drawImage(), and hoping that sombody can help me out.

The code blew is copied from a Java book, it can run in my computer, without any error message, but why the image "mypc01_64.png" can not be drawn on the JPanel?

package TwoD;

import java.awt.*;

import javax.swing.*;

/**

*

* @author Jack

*/

publicclass DisplayImageextends JFrame{

/** Creates a new instance of DisplayImage */

public DisplayImage(){

add(new ImageCanvas());

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String[] args){

// TODO code application logic here

JFrame frame =new DisplayImage();

frame.setTitle("DisplayDemo");

frame.setSize(300,300);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class ImageCanvasextends JPanel{

ImageIcon imageIcon =new ImageIcon("/TwoD/mypc01_64.png");

Image image = imageIcon.getImage();

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

if(image!=null){

g.drawImage(image,0,0,getWidth(), getHeight(),this);

}

}

}

the source code and the image are in the same folder named "TwoD".

[2578 byte] By [jack_hzya] at [2007-11-27 9:30:14]
# 1
tr thisImageIcon imageIcon = new ImageIcon("mypc01_64.png");regardsAniruddha
Aniruddha-Herea at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for your reply, AniruddhaI had tried your way, but the only thing I can see is a blank window.Actually I had tried to use relative and absolute paths as well, not working.Do you thing the reason is the size or type of the picture?
jack_hzya at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...
# 3

hi,

the same code is just working fine with me. i have just changes the image to a physical image that is present in my machine, and i have tried with absolute path. make sure you can see the image. it has got something. i have tried with Sunset.jpg provided with xp.

ImageIconimageIcon= new ImageIcon("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.jpg");

regards

Aniruddha

Aniruddha-Herea at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...
# 4
Yes!!!! I can see the image!!!!! @_@But why does not the relative path work on my computer?This code is going to be a part of my application, so I think I may need to use the relative path, what should I do? don't say I have to reinstall my XP. T_T
jack_hzya at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...
# 5

hi

you said you can't see the image using absolute path as well. is it still the same scenario? and once you can see the absolute, you will sure be able to see relative. do not worry. are you using any editor? another thing, this depends on application environment as well. can you get any resource from that location using

ClassLoader.getSystemResource("abc.txt")

check these.

regards

Aniruddha

Aniruddha-Herea at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...
# 6

somehow the absolute path working within my application now, but how about the relative path? I am using Netbeans 5.5.1.

And I am sorry I don't understand how to use

ClassLoader.getSystemResource("abc.txt")

should I just copy and paste it somewhere in the code?

Thanks for your patience, Aniruddha

jack_hzya at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hi Aniruddha, I found this from google:

http://neptune.netcomp.monash.edu.au/JavaHelp/howto/pathnames/index.html

it explains why the relative path does not work with BlueJ, and I have the NetBeans+BlueJ installed in my computer. I am not sure if this is the reason yet, but I will try and see if the problem can be solved.

Million thanks to you, Aniruddha

jack_hzya at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...
# 8
hi!these link may help you, and read the java API http://java.sun.com/j2se/1.4.2/docs/api/java/lang/class-use/ClassLoader.html http://forum.java.sun.com/thread.jspa?threadID=582732&tstart=90regardsAniruddha
Aniruddha-Herea at 2007-7-12 22:41:04 > top of Java-index,Desktop,Core GUI APIs...