[英]Eclipse Error, Platformer Game
I'm having problems with my code. 我的代码有问题。 I just started with Eclipse.
我刚开始使用Eclipse。 Right now, I'm currently working on a platformer game with Java.
现在,我目前正在使用Java开发平台游戏。 I'm having problems with the Homestuck code.
我在Homestuck代码方面遇到了问题。 Which is below public static void main(String[] args).
低于public static void main(String [] args)。 Thanks for your help guys!
感谢您的帮助!
Error: 错误:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at projectOne.<init>(projectOne.java:31)
at projectOne.main(projectOne.java:104)
Code: 码:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class projectOne extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
GameEvents gameEvents = new GameEvents();
Timer gameTimer = new Timer(1, gameEvents);
int i = 0;
int horizontalposition = 500;
int verticalposition = 500;
BufferedImage Picture;
//Don't forget to declare your variables!
projectOne()
{
gameTimer.start();
this.addKeyListener(gameEvents);
try
{
Picture = ImageIO.read(getClass().getResource("Homestuck.gif"));
//The format for this is Picture = ImageIO.read(getClass().getResource("NameOfFile.typeoffile"));
}
catch (IOException e)
{
System.out.println("Pictures failed to load");
}
}
@Override
protected void paintComponent(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(0,0,this.getWidth(), this.getHeight());
g.setColor(Color.red);
///g.drawImage(Picture, horizontalposition, verticalposition, 100, 150, null);
g.drawImage(Picture, 50, 100, 500, 600, null);
//Here's the format you must follow when drawing simple Java Graphics objects
//g.fillOval(horizontal location, vertical location, width, height)
}
public class GameEvents implements ActionListener, KeyListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
repaint();
}
@Override
public void keyPressed(KeyEvent key) //stuff inside here happens when a key is pressed
{
if(key.getKeyChar()=='d')
{
horizontalposition=horizontalposition+20;
}
if(key.getKeyChar()=='s')
{
verticalposition=verticalposition+20;
}
if(key.getKeyChar()=='w')
{
verticalposition=verticalposition-20;
}
if(key.getKeyChar()=='a')
{
horizontalposition=horizontalposition-20;
}
if(horizontalposition<0)
{
horizontalposition=0;
}
System.out.println(key.getKeyChar());
System.out.println('d');
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
}
public static void main(String[] args)
{
JFrame f = new JFrame("Java Graphics Example Project");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
projectOne p = new projectOne();
f.setSize(1500,700);
f.add(p);
f.setVisible(true);
p.requestFocusInWindow();
}
}
In this line 在这条线
Picture = ImageIO.read(getClass().getResource("Homestuck.gif"));
the getResource call must be returning null. getResource调用必须返回null。 I suggest isolating that as a local variable to make it easier to debug:
我建议将其隔离为局部变量,以使其更易于调试:
File imageFile = getClass().getResource("Homestuck.gif");
if (imageFile == null) {
//consider throwing an exception here
} else {
Picture = ImageIO.read(imageFile);
}
试试这个,使用ResourceAsStream:
Picture = ImageIO.read(this.getClass().getResourceAsStream("Homestuck.gif"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.