【问题标题】:I am trying to set a background image in Eclipse using Java我正在尝试使用 Java 在 Eclipse 中设置背景图像
【发布时间】:2014-04-05 05:21:07
【问题描述】:

我正在尝试使用 Java 在 Eclipse 中设置背景图像,我认为我已经完成了大部分工作。

我正在尝试制作 2D 游戏,我想将背景图片添加到我的menuJFrame,您将在下面看到。

我已经有了这个代码:

这是我的主要课程JFrames

 import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class JFrames extends JPanel implements ActionListener {
    JFrame menuJFrame,howToPlayJFrame, level1JFrame;
    JPanel menuJPanel,howToPlayJPanel;
    JButton howToPlayButton,backToMainMenuButton,startGameButton, quitProgramButton;
    JLabel howToPlayLabel;

    public static void main(String [] args){
        JFrames jframes = new JFrames();
    }
    public JFrames(){
        //menuJFrame
        menuJFrame = new JFrame("SquareRun/Menu");
        menuJFrame.setVisible(true);
        menuJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menuJFrame.setSize(800, 600);
        //menuJPanel
        menuJPanel = new JPanel();
        menuJFrame.add(menuJPanel);
        howToPlayButton = new JButton("How To Play");
        menuJPanel.add(howToPlayButton);
        howToPlayButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) 
            {
                if (e.getSource() == howToPlayButton){
                    howToPlayJFrame.setVisible(true);
                } 
            }});
        startGameButton = new JButton("Start Game");
        menuJPanel.add(startGameButton);
        startGameButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) 
            {
                if (e.getSource() == startGameButton)
                    level1JFrame.setVisible(true);
                menuJFrame.setVisible(false);
            }});
        quitProgramButton = new JButton("Quit Game");
        menuJPanel.add(quitProgramButton);
        quitProgramButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) 
            {
                if (e.getSource() == quitProgramButton){
                    menuJFrame.dispose();
                }
            }});
        //howToPlayJFrame
        howToPlayJFrame = new JFrame("SquareRun/HowToPlay");
        howToPlayJFrame.setVisible(false);
        howToPlayJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        howToPlayJFrame.setSize(600, 100);
        //howToPlayJPanel
        howToPlayJPanel = new JPanel();
        howToPlayJFrame.add(howToPlayJPanel);
        howToPlayLabel = new JLabel("Use the arrow keys to move, Up= jump, Down= down, Right= right, Left= left");
        howToPlayJPanel.add(howToPlayLabel);
        backToMainMenuButton = new JButton("Close Window");
        howToPlayJPanel.add(backToMainMenuButton);
        backToMainMenuButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) 
            {
                if (e.getSource() == backToMainMenuButton){
                    howToPlayJFrame.setVisible(false);
                    howToPlayJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }}});
        //level1JFrame
        level1JFrame = new JFrame("Level 1");
        level1JFrame.setVisible(false);
        level1JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        level1JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
    public void actionPerformed(ActionEvent e) {
    }
    public Object getCurrentLevel() {
        return null;
    }

}

这是我的背景类:

import java.awt.Image;

import javax.swing.ImageIcon;

public class Background extends JFrames {
    private JFrames game;
    private Image image;

    public Background(JFrames game){
        this.game = game;
        image = (new ImageIcon("Image001.png")).getImage();

    }
}

【问题讨论】:

  • 你的问题是?
  • 看看这个问题,尤其是 Michael Myers 的回答:stackoverflow.com/questions/1064977/… Background 类不是正确的方法,而是使用 JComponent 之后设置的JFrame#setContentPane 方法。

标签: java eclipse image swing background


【解决方案1】:

您可以使用此代码获取图像

image = (new ImageIcon(this.getClass().getResource("Image001.png"))).getImage();

假设带有图像的文件与此类位于同一文件夹中。

【讨论】:

    【解决方案2】:

    不要创建以J 开头的类,这只会令人困惑,尤其是当有一个JFrame 类并且您创建了一个名为JFrames 的类时,它从JPanel 扩展...现在我'我只是困惑...

    您遇到的第一个问题是您实际上并没有使用或添加您的 image 到您的 Background 类中的任何内容...

    您问题的基本解决方案是使用 JLabel 作为背景组件,为其提供布局管理器并将您的组件添加到其中。

    public class Background extends JLabel {
    
        public Background() {
            setIcon(new ImageIcon("Image001.png")));
            // Don't forget to set the layout...
        }
    }
    

    然后您只需根据需要设置布局管理器并将组件添加到其中。

    这样做的问题是,在调整组件大小时它不会调整图像大小。为此,您需要提供一种方法来控制图像的绘制。

    这将要求您从 JPanel 类扩展您的 Background 类并覆盖它的 paintComponent 方法,按照您认为合适的方式绘制图像。

    查看Performing Custom Painting了解更多详情。

    这需要为图像提供某种缩放操作。虽然在 Java 中有多种方法可以缩放图像,但您需要注意一些问题。看看The Perils of Image.getScaledInstance()

    这引发了一系列新问题,您想缩放它们并保持纵横比吗?如果是这样,您是要让图像适合可用区域还是填充它(这样它总是会覆盖可用空间)?

    查看Java: maintaining aspect ratio of JPanel background image了解更多详情。

    我还强烈建议您查看The Use of Multiple JFrames: Good or Bad Practice?http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html,这将帮助您产生不那么混乱的用户体验,恕我直言。

    您还应该看看Reading/Loading an Image 作为ImageIcon 的替代品,因为它具有读取更多文件格式的能力,但在无法读取图像时也会抛出IOException,这在诊断丢失图像问题方面非常有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-04
      • 2022-01-01
      • 1970-01-01
      • 2010-10-06
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多