カードレイアウトで画像を描画する際に発生した問題を説明する以前の投稿を作成しました。限られたサポートしか受けられず、問題を解決できませんでした。そのため、有効な助けが得られることを期待して、この投稿を再度説明しています。
Java ゲームのタイトル画面を作ろうとしています。私は2つのJavaファイルを作りました。最初のファイルは cardlayout をロードし、次のファイルはタイトル画面をロードします。しかし、どういうわけか、2番目のファイルで背景画像を描画できません。エラー メッセージは表示されません。単に何も描画しません。
最初のファイル:
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
Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
public MainScreen() {
JFrame frame = new JFrame();
frame.setTitle("Project");//Title of the screen
frame.setSize(800,600);//Size of the window
frame.setResizable(false);//Is the window resizable?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the frame when the default red cross is clicked
frame.setVisible(true);//is the frame visible?
frame.getContentPane().add(cardContainer, BorderLayout.CENTER);//add the cardcontainer to flip panels
frame.setLocationRelativeTo(null);
}
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(null);//load the main menu
}
}
2 番目のファイル:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainMenu extends JPanel{
Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
public MainMenu(MainScreen frame){
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
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 paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(background, 0, 0, null);
repaint();
}
}
編集
画像表示の問題があるコードの正確な部分を SSCCE に抽出できますか? また、使用されている画像へのリンクも添付してください。破損している可能性があります。その場合、問題はコード内にありません。
どうぞ
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
Image background = Toolkit.getDefaultToolkit().createImage("data/images/title.png");//loads the background image
public MainScreen() {
JFrame frame = new JFrame();
frame.setTitle("Project");//Title of the screen
frame.setSize(800,600);//Size of the window
frame.setResizable(false);//Is the window resizable?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the frame when the default red cross is clicked
frame.setVisible(true);//is the frame visible?
frame.getContentPane().add(cardContainer, BorderLayout.CENTER);//add the cardcontainer to flip panels
frame.setLocationRelativeTo(null);
}
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??
JPanel titlePanel = new JPanel(new BorderLayout());//Debug panel, not used for anything, script does not work if not here
titlePanel.setBackground(Color.BLACK);//set the background color to black
String title = "Title Panel";//name the card or something like that
cardContainer.add(titlePanel, title);//add the card to the panel
cardCombo.addItem(title);//add the item??
cardLayout.show(cardContainer, title);//choose what card to show. For this instance show the mainmenu
}
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
}
//Here the i try to paint the image. No error messages show.
public void paintComponent(Graphics g){//<-------------HERE
g.drawImage(background, 0, 0, null);//<-------------HERE
repaint();//<-------------HERE
}
}