Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 敌方图形IO异常_Java_Ioexception - Fatal编程技术网

Java 敌方图形IO异常

Java 敌方图形IO异常,java,ioexception,Java,Ioexception,我在一个侧滚射击游戏上工作,当我试图上传我的飞船图片的图像时,它要么说未报告的异常,要么说异常;必须捕获或声明为抛出,否则它将表示表达式和接口、类或枚举的非法开始,我的许多代码行都需要这样做 类ship1扩展了JPanel实现ActionListener{ Image image = null; try { image = ImageIO.read(new File("ship1.png")); } catch (IOException e) {

我在一个侧滚射击游戏上工作,当我试图上传我的飞船图片的图像时,它要么说未报告的异常,要么说异常;必须捕获或声明为抛出,否则它将表示表达式和接口、类或枚举的非法开始,我的许多代码行都需要这样做

类ship1扩展了JPanel实现ActionListener{

Image image = null;


    try {
        image = ImageIO.read(new File("ship1.png"));
    } catch (IOException e) {
        System.out.println("err");
    }

Timer t = new Timer(5, this);
int x = 800, y = 250;
double xVel = -2, yVel = 0;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, x, y, null);
    t.start();
}

public void actionPerformed(ActionEvent e) {
    x += (int) xVel;
    yVel = 5 * Math.sin(Math.toDegrees(x) / 750);
    y += (int) yVel;
    repaint();
}

}

你不能把
try
/
catch
块像这样放在类声明中-它必须放在构造函数或方法中。在绘制方法中启动计时器是一个非常糟糕的主意。您不知道何时调用paintComponent。例如,当窗口移动时,当屏幕保护程序启动时,甚至当用户将鼠标指针移到窗口上时,都可以调用它。绘画方法每秒调用几次并不少见。谢谢你的帮助。