Java背景框架和图像之间的差距

时间:2013-12-23 18:51:19

标签: java swing paintcomponent repaint

通过各种布局/空值和其他方法过渡乐趣,试图使我的背景图像完美地适合我的框架,而没有令人烦恼的空白,我到目前为止无法清除。每次尝试都会导致更大的差距。如果布局设置为左侧,则图像不可避免地填充左侧的间隙,但是加宽右侧的间隙,最后一次尝试通过将图像添加到框架中直接填充它,但导致屏幕上的其他元素不再接收重画调用

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,强制图像的位置或以下教程导致没有真正的变化。非常感谢帮助,并确保我再也不会遇到这样的问题。

1 个答案:

答案 0 :(得分:2)

调用setResizable会更改框架的大小。

相反,请先尝试拨打setResizable,然后再packsetVisible

例如......

<强>更新

持续的“填充”是由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);            
        }
    }
}
相关问题