【发布时间】:2017-11-15 20:43:13
【问题描述】:
为什么会这样:
URL url = MinecraftPlatformGame.class.getResource("images/diamondPick.png");
image = Toolkit.getDefaultToolkit().getImage(url);
int width = image.getWidth(null);
int height = image.getHeight(null);
System.out.println(width);
System.out.println(height);
为width 和height 返回-1
编辑:在我想出答案之前我的问题实际上是我应该如何解决它。 我通过执行以下操作修复了它:
URL url = MinecraftPlatformGame.class.getResource("images/diamondPick.png");
image = Toolkit.getDefaultToolkit().getImage(url);
MediaTracker mTracker = new MediaTracker(this);
mTracker.addImage(image,1);
try {
mTracker.waitForID(1);
} catch (InterruptedException ex) {
Logger.getLogger(MinecraftPlatformGame.class.getName()).log(Level.SEVERE, null, ex);
}
int width = image.getWidth(null);
int height = image.getHeight(null);
System.out.println(width);
System.out.println(height);
url = MinecraftPlatformGame.class.getResource("images/gui.png");
image1 = Toolkit.getDefaultToolkit().getImage(url);
mTracker.addImage(image1,2);
try {
mTracker.waitForID(2);
} catch (InterruptedException ex) {
Logger.getLogger(MinecraftPlatformGame.class.getName()).log(Level.SEVERE, null, ex);
}
width = image1.getWidth(null);
height = image1.getHeight(null);
System.out.println(width);
System.out.println(height);
我现在遇到的问题是这似乎不是很有效,而且我似乎不需要这么多代码来导入两个图像并给定尺寸。有没有更好、更有效、更简单的方法来做到这一点?
【问题讨论】:
标签: java