[英]Java IO can't read input file?
我剛剛開始學習Java,並且一直在為帶有鍵盤輸入的移動對象編寫此代碼。 我現在正在嘗試添加背景,但是它始終會出現以下錯誤:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at game.GameLoop.run(GameLoop.java:24)
at java.lang.Thread.run(Unknown Source)
我在Game.java中擁有的代碼是:
package game;
import java.applet.*;
import java.awt.*;
public class Game extends GameLoop{
public void init(){
setSize(864,480);
Thread th = new Thread(this);
th.start();
offscreen = createImage(864,480);
d = offscreen.getGraphics();
addKeyListener(this);
}
public void paint(Graphics g){
d.clearRect(0, 0, 864, 480);
d.drawImage(background, 0, 0, this);
d.drawRect(x, y, 20, 20);
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g){
paint(g);
}
}
這是我的GameLoop.java:
package game;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class GameLoop extends Applet implements Runnable, KeyListener{
public int x, y;
public Image offscreen;
public Graphics d;
public boolean up, down, left, right;
public BufferedImage background;
public void run(){
x = 100;
y = 100;
try {
background = ImageIO.read(new File("background.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
while(true){
x = 100;
y = 100;
while(true){
if (left == true){
x-=4;
}
if (right == true){
x+=4;
}
if (up == true){
y-=4;
}
if (down == true){
y+=4;
}
if ( x <= 0 ){
x = 0;
}
if ( y <= 0 ){
y = 0;
}
if ( x >= 843 ){
x = 843;
}
if ( y >= 460 ){
y = 459;
}
repaint();
try{
Thread.sleep(20);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
//@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == 37){
left = true;
}
if(e.getKeyCode() == 38){
up = true;
}
if(e.getKeyCode() == 39){
right = true;
}
if(e.getKeyCode() == 40){
down = true;
}
}
//@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == 37){
left = false;
}
if(e.getKeyCode() == 38){
up = false;
}
if(e.getKeyCode() == 39){
right = false;
}
if(e.getKeyCode() == 40){
down = false;
}
}
//@Override
public void keyTyped(KeyEvent e) {
}
}
抱歉,我似乎無法在``''中獲得全部信息,而且我還將修復凌亂的代碼,但是你們有什么想法導致此錯誤嗎,src目錄中有一個名為background的文件.png,它非常基礎,並且可以使用MS paint制作,如果有幫助的話。
謝謝。
有兩個簡單的沙盒小程序可以獲取圖像。
"background.png"
強烈表示第二種情況-應用程序的一部分。 本身'。
兩種類型的資源均應通過URL
標識( 請勿嘗試建立File
因為在部署applet時File
將失敗)。
獲取第二種情況的URL
的方式大致如下:
URL urlToBG = this.getClass().getResource("/path/to/the/background.png");
..其中/path/to/the/
可能只是/resources/
或/images/
。 它是類路徑上Jar中的路徑,可以在其中找到圖像。
大多數將加載File
都被重載以接受URL
。 這尤其適用於ImageIO.read(URL)
。
盡管Applet
類具有用於加載圖像的內置方法,但我建議堅持使用ImageIO
因為它可以提供更全面的故障反饋。
Thread.sleep(20);
不要阻止EDT(事件調度線程)-發生這種情況時,GUI將“凍結”。 而不是調用Thread.sleep(n)
實現用於重復任務的Swing Timer
或用於長時間運行的任務的SwingWorker
。 有關更多詳細信息,請參見Swing中的並發 。
這是第三個千年,是開始使用Swing而不是AWT的時候了。 那將意味着擴展JApplet
而不是Applet
。 然后,您可能會將繪畫邏輯轉移到默認情況下為雙緩沖的JPanel
中,並且可以在applet或框架(或窗口或對話框)中使用。
setSize(864,480);
小程序的大小是用HTML設置的,小程序應接受它分配的任何大小並在其中工作。 考慮到這一點,語句如下:
d.clearRect(0, 0, 864, 480);
..應該閱讀更多像:
d.clearRect(0, 0, getWidth(), getHeight());
@Patricia Shanahan您的評論實際上幫助我解決了同樣的問題。
我已經使用了該代碼:
File here = new File(".");
try {
System.out.println(here.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
從那里您可以找出正確的使用路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.