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".

