很简单,我无法绘制此图像。`Graphics.drawImage()`不会绘制
public class RenderMap extends JPanel {
static BufferedImage brick;
static BufferedImage groundb;
public static void main(String[] args) {
JFrame window = new JFrame("Super Mario");
RenderMap content = new RenderMap();
window.setContentPane(content);
window.setBackground(Color.WHITE);
window.setSize(1200, 800);
window.setLocation(100,0);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setVisible(true);
try {
brick = ImageIO.read(new File("SuperMario/brick.png"));
} catch (IOException e) {
}
try {
URL url = new URL("SuperMario/brick.png");
brick = ImageIO.read(url);
} catch (IOException e) {
}
try {
groundb = ImageIO.read(new File("SuperMario/ground.png"));
} catch (IOException e) {
}
try {
URL url = new URL("SuperMario/ground.png");
groundb = ImageIO.read(url);
} catch (IOException e) {
}
}
public Ground ground;
public RenderMap() {}
public void paintComponent(Graphics g) {
if(ground == null) {
ground = new Ground();
}
ground.draw(g);
}
public class Ground implements ImageObserver {
Ground(){}
void draw(Graphics g) {
g.drawImage(groundb, 0, 0, 1200, 800, this);
g.fillOval(8, 8, 16, 16);
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
// TODO Auto-generated method stub
return false;
}
}
}
这绘制了椭圆形,但不是图像。我试图改变到另一个图像(即在另一个程序中运行),它仍然无法工作,所以我知道这不是图像的问题。
伙计,这看起来像你从学校做的功课!无论如何,你可以检查'.read()'图像时是否抛出'IOException'。 “catch”块都是空白的,所以你不知道它是否成功加载图像文件。 –
@lanthe:我不认为它是家庭作业 - 该文件夹命名为'SuperMario' :) @chris:尝试'在你的catch块中的e.printStackTrace()'作为lanthe建议 - 之后,在这里发布输出。 – chris
@lanthe:是的,这是一个学校项目! –