我尝试了许多解决方案,但我似乎无法找到我的代码的问题。我检查了路径名、文件的权限,并为图像尝试了几种不同的文件名。图像不显示。
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();
}
}
}
请帮忙...