Load image resources in Java








Suppose you place icon.gif file in com.xxx.app.resources folder, you want to load the image in com.xxx.app.YourTest.java class.

You can:

import java.awt.Toolkit;
import java.awt.Image;
public class YourTest {
public void loadImage(){
Image image = Toolkit.getDefaultToolkit().createImage(
StoreSCP.class.getResource("resources/icon.gif"));
... ...
}
}




or you can:



import java.awt.Toolkit;
import java.awt.Image;
import java.net.URL;
public class YourTest {
public void loadImage(){
URL url = ClassLoader.getSystemResource("com/xxx/app/resources/icon.gif");
Image image = Toolkit.getDefaultToolkit().createImage(url);
... ...
}
}




No comments:

Post a Comment