【问题标题】:Java-Graphics-Importing a Background ImageJava-Graphics-导入背景图片
【发布时间】:2014-09-26 22:03:52
【问题描述】:

我第一次弄乱 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);
      }

    }

【问题讨论】:

标签: java swing jpanel paintcomponent imageicon


【解决方案1】:

一些事情

  1. 查看你正在调用的构造函数:

    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);
        }
    }
    

    因此,除了使用其大小之外,您实际上并没有对传递给构造函数的Image 做任何事情。您可能应该使用它来初始化backgroundImg。像这样:

    private Image backgroundImg;
    
    public ImagePanel(Image backgroundImg) {
        this.backgroundImg = backgroundImg;
    }
    
  2. 看看这几行

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

    仅供参考,frame.add()frame.getContentPane().add() 做同样的事情。在您的情况下,由于您没有更改框架/内容窗格的布局,因此您将使用默认的BorderLayout。这对您意味着,如果您在添加组件时未指定位置(例如frame.add(component, BorderLayout.PAGE_END)),那么您正在尝试添加到中心(例如frame.add(component, BorderLayout.CENTER))。问题在于每个 BorderLayout 位置只能容纳一个组件 - 您添加的最后一个组件获胜。所以在你的情况下game 获胜。

    有关布局管理的其他一些想法,请参阅Laying out Components Withing a Container。也更直接的看How to use BorderLayout

  3. Window 是一个 AWT 组件。您不应该尝试将 AWT 组件添加到 Swing 组件。最好只使用JPanelJComponent,虽然不太确定窗口的用途

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

  5. 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));
        }
    }
    

    也可以看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-14
    • 2014-07-03
    • 1970-01-01
    • 2011-02-11
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多