java 使用 getImage 加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14416751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading an Image using getImage
提问by InspiringProgramming
Here is a simple applet to try loading an image but it shows a blank window
这是一个尝试加载图像的简单小程序,但它显示一个空白窗口
import java.applet.*;
import java.awt.*;
public class Mama extends Applet {
int width, height;
Image img;
@Override
public void init(){
img = getImage(getCodeBase(), "C:\Users\......\Backgound.png");
}
@Override
public void paint(Graphics g){
g.drawImage(img, 0, 0, this);
}
}
I copied the path of the image from the directory, what am I doing wrong?
我从目录中复制了图像的路径,我做错了什么?
回答by Reimeus
Unless they are signed, applets can only read files from the same location from which they were loaded. Move the image to image to a location that is accessible relative to the class (or document) path and use:
除非经过签名,否则小程序只能从加载它们的同一位置读取文件。将图像移动到相对于类(或文档)路径可访问的位置并使用:
img = getImage(getCodeBase(), "Backgound.png");
回答by Данияр Мырзаканов
You can write as here
你可以写在这里
import java.applet.Applet;
import java.awt.*;
import java.net.URL;
public class SimpleImageLoad extends Applet {
Image img;
@Override
public void init() {
super.init();
img=getImage(getCodeBase(),"file:\D:\pic.PNG");
System.out.println(getCodeBase());
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img,0,0,this);
}
}