Jpanel尺寸不正确

时间:2016-06-16 02:08:18

标签: java swing jpanel

所以我试图使用swing,JFrame和JPanel制作类似Pacman的游戏,尺寸为256x256;然而,当我实际编译游戏时,屏幕有点大。我不知道是不是因为没有正确覆盖方法或可能处理布局的事情。

这是我的JFrame类,它添加了一个JPanel

*请注意我已经注释了一些内容,因为我试图弄清楚它们是否有帮助。

public class GameDevelopment extends JFrame{

//Window Stuff ... hopefully
public static final int DimensionHeight = 256;
public static final int DimensionWidth = 256;

// Level stuff possibly     
Tile startingTile;

Player player;
Walls walls;
public static Tile[][] tiles;

public GameDevelopment(){
    //Initialize           
    tiles = new Tile[DimensionWidth/16 ][DimensionHeight/16];
    for(int i = 0; i < DimensionWidth/16; i++){
        for(int y = 0; y < DimensionHeight/16; y++){
            tiles[i][y] = new Tile(i, y);
        }
    }

    startingTile = tiles[tiles.length/2][tiles[0].length/2];
    //xStart = (tiles.length/2) * 16;
    //yStart = (tiles[0].length/2) * 16;
    walls = new Walls();
    player = new Player(startingTile);


    //Sceen stuff       
    setTitle("Game");                        
    addKeyListener(player);
    addMouseListener(player);
    add(new Draw(walls, player), BorderLayout.CENTER);
    //setSize(DimensionWidth, DimensionHeight);
    pack();

    setLocationRelativeTo(null);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public static void main(String[] args){
    SpriteSheet.getSprites();
    //Create tiles
    GameDevelopment game = new GameDevelopment();
}

我知道如果你添加一个组件并调用pack(),它会将jframe设置为组件大小,所以我不确定是什么问题。

这是我绘制屏幕的Jpanel类。我还手动绘制了屏幕在矩形中的样子,屏幕明显比预期的要大。

*注意我传入了各种具有绘制方法的类,并使用一个paintComponent从这个类中调用这些方法,它可能没有很好地优化,但这应该不是问题。

public class Draw extends JPanel{

//Size
int DimensionHeight;
int DimensionWidth;

// Components
ArrayList components;
int amount;

public Draw(DrawGraphics ... _components){
    //Size
    DimensionWidth = GameDevelopment.DimensionWidth;
    DimensionHeight = GameDevelopment.DimensionHeight;
    //this.setPreferredSize(new Dimension(256, 256));
    //setMinimumSize(new Dimension(DimensionWidth, DimensionHeight));
    //setMaximumSize(new Dimension(DimensionWidth, DimensionHeight));
    //setSize(DimensionWidth, DimensionHeight);
    setBackground(new Color(80, 0, 255));
    System.out.println(getMinimumSize());

    //Components
    components = new ArrayList();
    components.addAll(Arrays.asList(_components));
    amount = components.size();
}

@Override
public Dimension getPreferredSize(){
    return new Dimension(DimensionWidth, DimensionHeight);
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    for(int i = 0; i < 16; i++){
        for(int y = 0; y < 16; y++){
            g.drawRect(i*16, y*16, 16, 16);
        }
    }
    for(int i = 0; i < amount; i++){
        ((DrawGraphics)components.get(i)).draw(g);
    }
    //g.drawRect(0, 0, 16, 16);
    repaint();
}

结果我得到了这张照片, 它在绘制的矩形后显示一些蓝色空间:

http://i.stack.imgur.com/gep0O.png

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

不要在绘画方法中调用repaint()。这将导致无限循环。

  

它在绘制的矩形后显示一些蓝色空格:

    setResizable(false);
    pack();
    setLocationRelativeTo(null);
//    setResizable(false);

在打包框架之前,您需要使框架不可调整大小。