Im trying to run:
import java.applet.Applet;
import java.awt.*;
import java.net.URL;
public class img extends Applet
{
private Image img;
public void init()
{
img = null;
}
public void loadImage()
{
try
{
img = getImage(getCodeBase(), "winter.jpg");
System.out.println(img);
System.out.println(prepareImage(img, 300, 400, this));
}
catch(Exception e){}
System.out.println(getDocumentBase());
}
public void paint(Graphics g)
{
if (img == null)
loadImage();
g.drawImage(img, 0, 0, this);
}
}
But it doesn't find winter.jpg unless its in: file:/C:/Users/Admin/AppData/Local/Temp/
System.out.println(getDocumentBase()); returns: file:/C:/Users/Admin/AppData/Local/Temp/AppletPage1228891259548967526.html Instead of: C:/Users/Admin/Dropbox/dev/idea/Exam3/out/production/Exam3/ (where the .class files are located)
I'm using IntelliJ IDEA 12.
I simply want to put my JPEGs in the Exam3 folder instead of the Temp folder. Any ideas?
A workaround for loading the resource using getDocumentBase() you can get the URL by using the getResource of class which gets the resources relative to the class.
URL base = img.class.getResource("/data/winter.jpg");
Image img = ImageIO.read(base);
where data is a folder in the Exam3 folder in your case which contains this class file.
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.