一直在嘗試通過各種佈局/零點和其他方法轉換樂趣,試圖讓我的背景圖像完美地適合我的框架,而沒有令人討厭的空白,我迄今還未能清楚。每一次嘗試都會導致更大的差距。如果佈局設置爲左側,則圖像不可避免地會填滿左側的空白,但會擴大右側的空白,最後一次嘗試通過將圖像添加到框架直接填充它,但導致屏幕上的其他元素不再接收重繪調用Java框架和圖像之間的背景差距
在我的遊戲設置了一個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);
/**/}
////////////////////////////////////
}
在方案和實施例中使用的圖像(重命名爲背景) http://img716.imageshack.us/img716/6410/seq6.png
目前我已經達到了如果必須的時候,我會留下差距,因爲它不會影響整個遊戲,但是它讓我感到非常糟糕,並且每次嘗試交換佈局,消除hgap/vgap ,迫使圖像的位置或後續教程導致沒有真正的變化。幫助將不勝感激,並有助於確保我再也不會遇到這樣的問題。
1)爲了更好地幫助越早,張貼[SSCCE](http://sscce.org/)。 2)獲取圖像的一種方法是通過熱鏈接到[本答案](http://stackoverflow.com/a/19209651/418556)中看到的圖像。 –
我喜歡你用////和/ **/ –
組織你的代碼的方式我推測這兩個類組成了一個SSCCE,因爲它實際上是受影響的代碼的裸骨,還要學習如何做出更有效的SSCCE。此外,我猜想與圖像,這將有助於如果我鏈接原始背景圖像是你問。 – ulthanex