PDA

Click to See Complete Forum and Search --> : JAVA - load image from a local directory


BramVandenbon
Oct 9th, 2004, 10:48 AM
Loading images...


private Image loadImage (String pad){
return Toolkit.getDefaultToolkit().getImage(getClass().getResource(pad));
}


the getResource() function automatically searches for the file in your local directory, if it is not found there it will look inside the java system directories ... etc.

Short example:


public class MyPanel extends JPanel implements java.awt.image.ImageObserver{
public void paintComponent(Graphics g)
{
int x = 0;
int y = 0;
Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("mario.gif"));
g.drawImage(img, x, y, this);
}
}
}
}