我已尝试了一百万种不同的东西,但是当我告诉它显示图像时,Java只是给我错误或直接起作用。我必须使用默认的“图形”坐标网格,因为这将导致可玩的游戏。请告诉我(尽快 - 尽可能简单)如何使代码在下面工作。
import java.awt.Container;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class main extends JFrame {
static JFrame frame = new JFrame();
static Container pane = frame.getContentPane();
static Graphics graphics;
static ImageIcon background = new ImageIcon("background.png");
static JLabel backgroundLabel = new JLabel(background);
static File titleFile = new File("title.png");
static BufferedImage title;
public static void main(String[] args0) {
frame.add(backgroundLabel);
backgroundLabel.setVisible(true);
frame.setContentPane(backgroundLabel);
frame.setVisible(true);
frame.setTitle("Fastball");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
try{
title = ImageIO.read(titleFile);
} catch (IOException e) {
System.out.println(e);
}finally{
};
if(title != null) {
graphics.drawImage(title, 15, 15, 440, 112, null);
}
}
}
使用此代码,我收到错误Exception in thread "main" java.lang.NullPointerException at main.main(main.java:60)
并且我不确切地知道它引用的是什么......