Java图形导入背景图像

Java图形导入背景图像,java,swing,jpanel,paintcomponent,imageicon,Java,Swing,Jpanel,Paintcomponent,Imageicon,我第一次用java图形,经过大量的搜索,我似乎仍然无法成功地添加背景,我在一个单独的文件中预先绘制了我的背景图像,我正试图将其导入到我的项目中。。。。 任何帮助都将不胜感激 public static void startMenu(){ ImagePanel panel = new ImagePanel(new ImageIcon("C:/Background.jpg").getImage()); // Loads the background imag

我第一次用java图形,经过大量的搜索,我似乎仍然无法成功地添加背景,我在一个单独的文件中预先绘制了我的背景图像,我正试图将其导入到我的项目中。。。。 任何帮助都将不胜感激

    public static void startMenu(){
        ImagePanel panel = new ImagePanel(new ImageIcon("C:/Background.jpg").getImage());

        // Loads the background image and stores in img object
        Window game = new Window();
        JFrame frame = new JFrame();

        JButton startButton = new JButton("Start"); //makes a button with the text Start
        startButton.setBounds(300, 400, 89, 23);
        startButton.setVisible(true);

        frame.getContentPane().add(startButton);
        frame.getContentPane().add(panel);

        frame.add(game);
        frame.pack();
        frame.setTitle(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null); // starts it in the center of the
                                            // screen
        frame.setVisible(true);


        game.start(); // Initialize start method
    }
    public static void startButton(){

        }

}


class ImagePanel extends JPanel {

      private Image backgroundImg;

      public ImagePanel(String backgroundImg) {
        this(new ImageIcon(backgroundImg).getImage());
      }

      public ImagePanel(Image backgroundImg) {
        Dimension size = new Dimension(backgroundImg.getWidth(null), backgroundImg.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
      }

      public void paintComponent(Graphics g) {
        g.drawImage(backgroundImg, 0, 0, null);
      }

    }
几件事
  • 查看您正在调用的构造函数:

    new ImagePanel(new ImageIcon("C:/Background.jpg").getImage());
    
    然后查看构造函数和
    ImagePanel

    class ImagePanel extends JPanel {
    
        private Image backgroundImg;
    
        public ImagePanel(Image backgroundImg) {
            Dimension size = new Dimension(
                                 backgroundImg.getWidth(null),
                                 backgroundImg.getHeight(null));
            ...
        }
    
        public void paintComponent(Graphics g) {
            g.drawImage(backgroundImg, 0, 0, null);
        }
    }
    
    因此,除了使用图像的大小外,您实际上没有对传递给构造器的
    图像执行任何操作。您可能应该使用它来初始化
    backgroundImg
    。像这样:

    private Image backgroundImg;
    
    public ImagePanel(Image backgroundImg) {
        this.backgroundImg = backgroundImg;
    }
    
  • 看这些线

    frame.getContentPane().add(startButton);
    frame.getContentPane().add(panel);
    frame.add(game);
    
    仅供参考,
    frame.add()
    执行与
    frame.getContentPane().add()相同的操作。在您的情况下,由于您没有更改框架/内容窗格的布局,因此只剩下默认的
    BorderLayout
    。这对您来说意味着,如果您在添加组件时没有指定位置(如
    frame.add(组件,BorderLayout.PAGE_END)
    ),那么您将尝试添加到中心(如
    frame.add(组件,BorderLayout.center)
    )。问题在于,每个
    BorderLayout
    位置只能容纳一个组件,即您添加的最后一个组件。所以在你的情况下,
    游戏
    获胜

    有关布局管理的其他想法,请参见。更直接地,请参见

  • 是AWT组件。您不应该尝试将AWT组件添加到Swing组件。最好只使用
    JPanel
    JComponent
    ,尽管不能确定窗口的用途

  • g.drawImage(backgroundImg,0,0,null)
    null
    应改为
    this

  • ImagePanel
    构造函数中,去掉所有
    Get[Xxx]Size()
    。您不需要它们,最好只覆盖
    getPreferredSize()

    也看看


  • 这将有助于更快地获得更好的帮助,发布一个(最少完整的可验证示例)。
    class ImagePanel extends JPanel {
    
        private Image backgroundImg;
    
        public ImagePanel(Image backgroundImg) {
            this.backgroundImg = backgroundImg;
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(backgroundImage.getWidth(this),
                                 backgroundImage.getHeight(this));
        }
    }