编辑:This帖子表明这是IDE特定的问题,但给出的解决方案是IntelliJ。我正在使用Eclipse。
我正在关注Android游戏编程的this指南。但在此之前,它教授用Java进行游戏编程。
当我尝试在指南中指定的小程序中加载图像时,不显示任何内容,只显示黑色背景。我的源代码和网络代码几乎完全相同,我该如何解决?
这是我的代码:
package game;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
public class Incio extends Applet implements Runnable, KeyListener {
private Robot robot;
private Image image, character;
private Graphics second;
private URL base;
@Override
public void init() {
setSize(800, 480);
setBackground(Color.black);
setFocusable(true);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("LAS AVENTURAS DE RABOCOP");
addKeyListener(this);
robot = new Robot();
try {
base = getDocumentBase();
} catch (Exception e) {
// TODO: handle exception
}
character = getImage(base, "/src/data/Robot.bmp");
System.out.println(base);
}
@Override
public void start() {
// TODO Auto-generated method stub
}
@Override
public void stop() {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent k) {
switch (k.getKeyCode()) {
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_RIGHT:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_SPACE:
break;
}
}
@Override
public void keyReleased(KeyEvent k) {
switch (k.getKeyCode()) {
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_RIGHT:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_SPACE:
break;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
robot.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
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);
}
public void paint(Graphics g) {
g.drawImage(character, robot.getCenterX()-61, robot.getCenterY()-63, this);
}
}
感谢。