* 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
예외 메시지 인 TestPanel의 43 행에 표시된 코드 행을주의 깊게 살펴 봐야합니다. 그 행에 뭔가가 있습니다. –