2014-03-03 29 views
0

我想在Eclipse中使用Java設置背景圖像,我認爲我已經完成了大部分工作。我想在Eclipse中使用Java設置背景圖像

我想做一個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(); 

    } 
} 
+0

和你的問題是? –

+0

看看這個問題,特別是邁克爾·邁爾斯的回答:http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe背景類不是正確的方法,而是使用'JComponent',後面用'JFrame#setContentPane'方法設置。 – jlr

回答

1

你可以得到的圖像使用該代碼

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

假設帶圖像的文件與此類位於同一文件夾中。

1

不要創建類開始J,它只是混亂,尤其是當有一個JFrame類,你已經創建了一個名爲JFrames類從JPanel延伸......現在我只是困惑...

您遇到的第一個問題是,你不實際使用或您的Background類中添加image到什麼...

基本解決你的問題是使用一個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時,它無法讀取圖像,它可以是非常有幫助診斷丟失的圖像問題。