* A simple panel for testing various parts of our game.
* This is not part of the game. It's just for testing.
*/
package game;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* A simple panel for testing various parts of our game.
* This is not part of the game. It's just for testing.
*/
public class TestPanel extends JPanel
{
private static final long serialVersionUID = 1L; // Ignore this - It's just to get rid of a warning.
// Instance variable(s).
private Image backdrop;
/**
* Constructor - loads a background image
*/
public TestPanel ()
{
try
{
ClassLoader myLoader = this.getClass().getClassLoader();
InputStream imageStream = myLoader.getResourceAsStream("resources/path_1.jpg");
backdrop = ImageIO.read(imageStream);
// You will uncomment these lines when you need to read a text file.
InputStream pointStream = myLoader.getResourceAsStream("resources/ path_1.txt");
Scanner s = new Scanner (pointStream);
}
catch (IOException e)
{
System.out.println ("Could not load: " + e);
}
}
/**
* This paint meethod draws the background image anchored
* in the upper-left corner of the panel.
*/
public void paintComponent (Graphics g)
{
g.drawImage(backdrop, 0, 0, null);
}
/* Override the functions that report this panel's size
* to its enclosing container. */
public Dimension getMinimumSize()
{
return new Dimension (600, 600);
}
public Dimension getMaximumSize()
{
return getMinimumSize();
}
public Dimension getPreferredSize()
{
return getMinimumSize();
}
}
这段代码针对我正在为我的 Java 课程做的视频游戏作业。此类仅用于测试我们的代码。在分配的方向上,我被告知将存在于 try 块中的代码放在上面,如上所示。显然,代码应该打开我工作区文件夹中的 JPEG 图像。但是,当我尝试代码时,它只说明:
Exception in thread "main" java.lang.NullPointerException at
java.io.Reader.<init>(Unknown Source) at
java.io.InputStreamReader.<init>(Unknown Source) at
java.util.Scanner.<init>(Unknown Source) at
game.TestPanel.<init>(TestPanel.java:43) at
game.TestApplication.main(TestApplication.java:24)
我不完全清楚 inputStream 和 classLoaders 的作用。所以,如果你有任何关于这两个方面的基本信息,那就太好了。另外,我知道构造函数方法下面的其他方法中没有代码。我的作业指导没有说明我应该在这些方法中输入什么。
enter code here
enter code here