Java 2D ゲームのメイン メニューを作成しようとしています。メイン メニューとゲーム自体を切り替えるために、カード レイアウトを使用することを考えていました。しかし、このエラーに遭遇するまで、私はそこまで行きませんでした。タイトル画面の背景画像を描画しようとしたところ、このエラーが発生しました。2つのクラスを参照してみましょう。
最初のクラスは、JFrame とカード レイアウトを読み込みます。
import javax.swing.*;
import java.awt.*;
public class MainScreen extends JFrame{
public static CardLayout cardLayout = new CardLayout();//set a new cardlayout
// *** JPanel to hold the "cards" and to use the CardLayout:
static JPanel cardContainer = new JPanel(cardLayout);//some variable for the cardlayout
public static JComboBox cardCombo = new JComboBox();//some variable for the cardlayout
public static JPanel comboPanel = new JPanel();;//some variable for the cardlayout
public MainScreen() {
setTitle("Project");//Title of the screen
setSize(800,600);//Size of the window
setResizable(false);//Is the window resizable?
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the frame when the default red cross is clicked
setVisible(true);//is the frame visible?
getContentPane().add(cardContainer, BorderLayout.CENTER);//add the cardcontainer to flip panels
}
public static void debug(){//makes an extra card, this is a debug card and is not used for anything but debugging
JPanel debugPanel = new JPanel(new BorderLayout());//Debug panel, not used for anything, script does not work if not here
debugPanel.setBackground(Color.BLACK);//set the background color to black
String debug = "Debug Panel";//name the card or something like that
cardContainer.add(debugPanel, debug);//add the card to the panel
cardCombo.addItem(debug);//add the item??
}
public static void main(String[] args){//this runs when the script opens
new MainScreen();//run the main screen class, that loads the window and card layout
debug();//run the next class that initializes the mainmenu
new MainMenu();//load the main menu
}
}
メニュー画面を読み込み、背景画像の描画を試みるクラス:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class MainMenu extends JComponent{
Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
public MainMenu(){
JPanel menuPanel = new JPanel();//This is the menu panel, where the main menu is loaded
menuPanel.setBackground(Color.BLACK);//set the background color to black
String menu = "Menu Panel";//name the card or something
menuPanel.setLayout(new GridLayout(1, 1, 0, 0));
MainScreen.cardContainer.add(menuPanel, menu);//add the card to the panel
MainScreen.cardCombo.addItem(menu);//add the item??
MainScreen.cardLayout.show(MainScreen.cardContainer, menu);//choose what card to show. For this instance show the mainmenu
paint(null);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(background, 0, 0, null);
g2.finalize();
}
}
そして、これはプログラムの実行時に表示されるエラーです:
Exception in thread "main" java.lang.NullPointerException
at MainMenu.paint(MainMenu.java:34)
at MainMenu.<init>(MainMenu.java:29)
at MainScreen.main(MainScreen.java:37)
*編集*
ラインを外しました
paint(null);
プログラムを実行すると、エラー メッセージは表示されませんが、背景が描画されません。黒の背景のみが表示されます。完全に編集されたファイル:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class MainMenu extends JComponent{
Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
public MainMenu(){
JPanel menuPanel = new JPanel();//This is the menu panel, where the main menu is loaded
menuPanel.setBackground(Color.BLACK);//set the background color to black
String menu = "Menu Panel";//name the card or something
menuPanel.setLayout(new GridLayout(1, 1, 0, 0));
MainScreen.cardContainer.add(menuPanel, menu);//add the card to the panel
MainScreen.cardCombo.addItem(menu);//add the item??
MainScreen.cardLayout.show(MainScreen.cardContainer, menu);//choose what card to show. For this instance show the mainmenu
}
public void paintThis(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(background, 0, 0, null);
g2.finalize();
repaint();
}
}