我尝试了很多解决方案,但我似乎无法找到代码的问题。我检查了路径名,文件权限,并为图像尝试了几个不同的文件名。图像没有显示出来。
private Character player;
private Image image, sprite;
private URL base;
private Graphics second;
@Override
public void init() {
setSize(500,500);
setBackground(Color.white);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame)this.getParent().getParent();
frame.setTitle("Joules");
try {
base = getDocumentBase();
sprite = getImage(base,"kips.PNG");
} catch (Exception e) {
}
// sprite = getImage(base,"kips.PNG");
}
@Override
public void start() {
player = new Character();
Thread thread = new Thread(this);
thread.start();
}
@Override
public void update(Graphics g) {
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void paint(Graphics g) {
g.drawImage(sprite,player.getCenterX(),player.getCenterY(),this);
}
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(34);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
请帮忙......
答案 0 :(得分:0)
我刚遇到这个问题,发现getDocumentBase()返回的是一条巨大的路径,它是当前applet的基础,而不是项目。
相反,我使用getCodeBase()返回“../projectName/bin/”,因此导航回代码中相对路径中的一个文件夹。
base = getCodeBase();
sprite = getImage(base,"../kips.PNG");