2

我尝试了许多解决方案,但我似乎无法找到我的代码的问题。我检查了路径名、文件的权限,并为图像尝试了几种不同的文件名。图像不显示。

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();
        }
    }
}

请帮忙...

4

1 回答 1

0

我刚遇到这个问题,发现 getDocumentBase() 正在返回一个巨大的路径,它是当前小程序的基础,而不是项目。

相反,我使用了返回“../projectName/bin/”的 getCodeBase(),因此在代码中的相对路径中导航回一个文件夹。

base = getCodeBase();
sprite = getImage(base,"../kips.PNG");

于 2014-02-16T16:13:12.543 回答