我之前发表过一篇文章,解释了我在卡片布局中绘制图像时遇到的问题。我得到了有限的帮助,并没有解决问题。所以我再次发布这篇文章来解释它,希望能得到一些有效的帮助。
我正在尝试为我的 java 游戏制作标题屏幕。我制作了两个java文件。第一个文件加载卡片布局,下一个加载标题屏幕。但不知何故,在第二个文件中我无法让它绘制我的背景图像。我没有收到任何错误消息。它根本不画任何东西。
第一个文件:
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
}
}
第二个文件:
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
}
}
对于图片:http: //i.stack.imgur.com/F3nHF.png