Reputation: 55
So I'm trying to make a Pacman-like game using swing, JFrame, and JPanel, with dimensions of 256x256; however, when I actually compile the game the screen is a bit bigger. I cant tell if it's because of not overriding a method correctly or possibly something dealing with layouts.
This is my JFrame class which adds a JPanel
*Note I have some things commented out because I was trying to figure out if they helped or not.
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();
}
I know if you add a component and call pack() it sets the jframe to the components sizes so I'm not sure what's the problem.
This is my Jpanel class which draws the screen. I also manually drew what the screen should look like in rectangles and the screen clearly is larger than what it's supposed to be.
*Note I pass in various classes which have draw methods and call those methods from this class using a single paintComponent, It probably isn't very well optimized but that shouldn't be the problem.
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();
}
As a result I get this picture, It shows some blue space after the drawn rectangles:
Thanks for helping!
Upvotes: 0
Views: 287
Reputation: 324118
Don't invoke repaint() in a painting method. This will result in an infinite loop.
It shows some blue space after the drawn rectangles:
setResizable(false);
pack();
setLocationRelativeTo(null);
// setResizable(false);
You need to make the frame non-resizable BEFORE you pack the frame.
Upvotes: 1