2013-12-23 62 views
1

一直在嘗試通過各種佈局/零點和其他方法轉換樂趣,試圖讓我的背景圖像完美地適合我的框架,而沒有令人討厭的空白,我迄今還未能清楚。每一次嘗試都會導致更大的差距。如果佈局設置爲左側,則圖像不可避免地會填滿左側的空白,但會擴大右側的空白,最後一次嘗試通過將圖像添加到框架直接填充它,但導致屏幕上的其他元素不再接收重繪調用Java框架和圖像之間的背景差距

enter image description here

在我的遊戲設置了一個JFrame的那一刻,創建其擴展JPanel我的遊戲系統類,以及遊戲系統類創建其處理要麼開始屏幕或實際遊戲畫面元素內部類。在由系統創建的遊戲容器類中,背景被繪製。

縮短按下碼提取

主類:

public class Test extends JPanel { 

//////////////// 
////Variables // 
//////////////////////////// 
/**/private static Test test; 
/**/private static JFrame stage; 
/**/private static Container CurrentMenu; 
/**/private boolean isRunning = true; //used to state whether game is running 
//////////////////////////////////// 

//////////////// 
// initialize // 
//////////////// 
/**/public static void main(String[] args) { 
/**/stage = new JFrame("Touhou FBE");//creates game window 
/**/stage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//sets up default handler for closing window 
/**/test = new Test();//creates an object of our game 
/**/stage.getContentPane().add(test);//add game object to frame 
/**/stage.pack(); 
/**/stage.setVisible(true);//sets frame to visible 
/**/stage.setResizable(false); 
/**/test.Game_loop(); 
/**/} 
//////////////////////////// 

///////////////// 
// constructor // 
///////////////// 
/**/public Test(){ 
/**//////////////////////////////// 
/**/// create starting game menu // 
/**////////////////////////////////   ////////////////// 
/**/CurrentMenu = new Container(this,stage);  // swap GameMenu with GameContainer to be able to see difference with painting 
/**/this.add(CurrentMenu);     ////////////////// 
/**/} 
/////////////////////// 


    //////////////////////////////////////////// 
    // Game loop which manages object updates // 
    //////////////////////////////////////////// 
    /**/public void Game_loop(){ 
    /**/while (isRunning) { 
    /**/test.repaint(); 
    /**/} 
    /**/} 
    ///////////////////////////////////////////////// 

    ///////////////////////////////// 
    // Rendering canvas for images // 
    ///////////////////////////////// 
    /**/public void paint(Graphics g) { 
    /**/super.paint(g); //Refresh Panel 
    /**/} 
    /////////////////////////////////////////////////////// 

容器類:

////////////////////////// 
// game container class // 
////////////////////////// 
/**/public class Container extends JPanel{ 
/**/  
/////////////// 
//Variables // 
/////////////// 
/**/public Test Game; 
/**/public JFrame stage; 
/**/private Image img; 

//////////////// 
//Constructor // 
//////////////// 
/**/public Container(Test game, JFrame Stage){ 
/**/Game = game; 
/**/stage = Stage; 
/**/img = (new ImageIcon("resources/background.png").getImage()); 
/**/Dimension size = new Dimension(700, 400); 
/**/setPreferredSize(size); 
/**/setMinimumSize(size); 
/**/setMaximumSize(size); 
/**/setLayout(null); 
/**/setSize(size); 
/**/} 
/**/  
//////////////////////////// 

////////////////// 
//Paint sprite // 
////////////////// 
/**/public void paintComponent(Graphics g) { 
/**///super.paint(g); 
/**/g.drawImage(img, 0,0, this); 
/**/} 
//////////////////////////////////// 
} 

在方案和實施例中使用的圖像(重命名爲背景) enter image description here http://img716.imageshack.us/img716/6410/seq6.png

目前我已經達到了如果必須的時候,我會留下差距,因爲它不會影響整個遊戲,但是它讓我感到非常糟糕,並且每次嘗試交換佈局,消除hgap/vgap ,迫使圖像的位置或後續教程導致沒有真正的變化。幫助將不勝感激,並有助於確保我再也不會遇到這樣的問題。

+0

1)爲了更好地幫助越早,張貼[SSCCE](http://sscce.org/)。 2)獲取圖像的一種方法是通過熱鏈接到[本答案](http://stackoverflow.com/a/19209651/418556)中看到的圖像。 –

+0

我喜歡你用////和/ **/ –

+0

組織你的代碼的方式我推測這兩個類組成了一個SSCCE,因爲它實際上是受影響的代碼的裸骨,還要學習如何做出更有效的SSCCE。此外,我猜想與圖像,這將有助於如果我鏈接原始背景圖像是你問。 – ulthanex

回答

2

調用setResizable更改了框架的大小。

相反,嘗試打電話setResizable,再pack,該setVisible

例如...

更新

繼續的「填充」是由於TestJPanel延伸並且Container被添加到其中而造成的。

JPanel的默認佈局管理器是FlowLayout,默認情況下,默認情況下會在容器周圍添加5個像素。

根據您的需求,我要麼改變Test佈局管理器BorderLayout或者乾脆直接添加Container到幀(它使用BorderLayout默認)

比如...

enter image description here

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Image; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class TestResize { 

    private JFrame stage; 
    private Container CurrentMenu; 
    private boolean isRunning = true; //used to state whether game is running 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TestResize(); 
      } 
     }); 
    } 

    public TestResize() { 
     CurrentMenu = new Container(this, stage);  // swap GameMenu with GameContainer to be able to see difference with painting 
     stage = new JFrame("Touhou FBE");//creates game window 
     stage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//sets up default handler for closing window 
     stage.add(CurrentMenu); 
     stage.setResizable(false); 
     stage.pack(); 
     stage.setVisible(true);//sets frame to visible 
     // This needs to be started in a separate thread 
//  test.Game_loop(); 
    } 

    public void Game_loop() { 
//  while (isRunning) { 
//   test.repaint(); 
//  } 
    } 

    public class Container extends JPanel { 

     public TestResize Game; 
     public JFrame stage; 
     private Image img; 

     public Container(TestResize game, JFrame Stage) { 
      Game = game; 
      stage = Stage; 
      img = (new ImageIcon("resources/background.png").getImage()); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      Dimension size = img == null ? new Dimension(700, 400) : new Dimension(img.getWidth(this), img.getHeight(this)); 
      return size; 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(img, 0, 0, this); 
      g.setColor(Color.RED); 
      g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);    
     } 
    } 
} 
+0

+1,您似乎是正確的。至少在我的幾個測試運行不同的應用程序的情況下 –

+0

正如你在上面提到的,在這個問題中,通過改變setResizable方法的順序,左/右和底部間隙現在被填充,但是上面的4-5px間隙是還在那兒。有一件事我通過使用getter發現,實際上圖像是在(0,0)處繪製的,所以無論出於何種原因,JFrame上的實際類被分流了5px,這與佈局有關? – ulthanex

+0

您錯過了一部分代碼(測試),因此無法確定。這可能是因爲有邊框或者測試的首選尺寸大於圖像。我不知道你爲什麼重寫paint,因爲它沒有做任何事情,你應該調用super.paintComponent來確保在繪製圖形上下文之前它已經做好了準備...... – MadProgrammer