[英]java applet cant load more than 1 image
我試圖在applet中制作一個java游戲時遇到了問題。
我無法加載超過1個圖像,否則將無法加載。
我正在獲取jar文件的圖像。
代碼加載器:
public BufferedImage LoadTex(String ura) {
BufferedImage res = null;
try {
URL url = this.getClass().getClassLoader().getResource("tex/" + ura);
res = ImageIO.read(url);
} catch (IOException e) {
}
return res;
}
代碼小程序:
tex texu = new tex();
BufferedImage plr;
BufferedImage hud_right;
BufferedImage hud_bottom;
@Override
public void init() {
plr = texu.LoadTex("tspr.png");
hud_right = texu.LoadTex("hud_right.png");
hud_bottom = texu.LoadTex("hud_bottom.png");
}
@Override
public void paint(Graphics screen) {
Graphics2D G2D = (Graphics2D) screen;
G2D.drawImage(hud_right, 570, 0, null);
G2D.drawImage(hud_bottom, 0, 410, null);
}
它與1張圖片完美配合,但如果我嘗試更多它停止。 客戶端甚至不會加載。
它給出了錯誤:input == null
如何解決這個問題。
謝謝
你永遠不應該消費異常,至少你應該記錄它們,這將節省你的頭發拉動時間......
public BufferedImage LoadTex(String ura) throws IOException {
BufferedImage res = null;
URL url = this.getClass().getClassLoader().getResource("tex/" + ura);
res = ImageIO.read(url);
return res;
}
你必須調用super.paint(g)
,paint方法在后台做了大量的工作,你永遠不應該忽略它。
public void paint(Graphics screen) {
super.paint(screen);
Graphics2D G2D = (Graphics2D) screen;
G2D.drawImage(hud_right, 570, 0, null);
G2D.drawImage(hud_bottom, 0, 410, null);
}
嘗試單獨加載每個圖像並使每個圖像都可以加載。 如果這樣做並且您仍然無法加載一個圖像,則可能存在內存問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.