简体   繁体   中英

Graphics.drawImage doesn't draw in Java

I am trying to draw images with g.drawImage(), However, it does not display on the screen. So can you please examine my code and tell me what the reason may be?

private char enemyBoard[][] = new char[10][10]; 
private Rectangle r[][] = new Rectangle[10][10];
private int size;  
Image img;
public EnemyPanel()
{
    size=Constant.rectSize;
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            enemyBoard[i][j]='*'; //initialization type
            r[i][j]= new Rectangle(j*size+30,i*size+30, size, size);         
        }
    }

    img=Toolkit.getDefaultToolkit().createImage("/edu/iyte/ceng316/resource/kurukafa2.jpg";);

}


  public void paintComponent(Graphics g){     
        super.paintComponent(g);

        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++)
                    g.drawImage(img,(int)r[i][j].getX(),(int)r[i][j].getY(),null);                                                          
            }
        }        
  }
public void loadImage()
   {
     try
     {
     img=ImageIO.read(this.getClass().getResource(Constant.kurukafa));
     }
     catch(IOException e)
     {
     e.printStackTrace();
     } 
   }

Okey I solved my problem with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM