help: JPanel/JFrame display complexities, wrong size

I am trying to make a simple game of worms.I wan't to draw a rectangle in the center of the window where in the middle the game is played and on the outside score, lives and other such things are displayed. The problem is the rectangle won't draw properly because the window is the wrong size and I don't know if it is something I am doing wrong with the panel or frame.

import javax.swing.*;

import java.awt.*;

publicclass DemoWormPanelextends JPanelimplements Runnable{

privatestaticfinalint WIDTH = 600, HEIGHT = 400;

private Graphics dbG;

private Image dbImage;

private Thread animator;

privateboolean running =false;

private Rectangle walls;

public DemoWormPanel(){

super();

setSize(WIDTH,HEIGHT);

}

publicvoid startGame(){

if (animator ==null){

animator =new Thread(this);

animator.start();

}

running =true;

//defining game walls at 50 pixels within panel border

walls =new Rectangle(50, 50, WIDTH - 50, HEIGHT - 50);

}

publicvoid gameRender(){

if (dbImage ==null){

dbImage = createImage(WIDTH, HEIGHT);

if (dbImage ==null){

System.out.println("Error creating double buffer");

System.exit(0);

}

dbG = dbImage.getGraphics();

}

dbG.setColor(Color.black);

dbG.fillRect(0,0,WIDTH,HEIGHT);

dbG.setColor(Color.white);

dbG.drawRect(walls.x, walls.y, walls.width, walls.height);

}

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

g.drawImage(dbImage, 0, 0,this);

g.dispose();

}

publicvoid update(Graphics g){

paint(g);

}

publicvoid run(){

while (running){

gameRender();

repaint();

try{

Thread.sleep(1000/50);

}catch (InterruptedException e){}

}

System.exit(0);

}

publicstaticvoid main(String[] args){

DemoWormPanel wp =new DemoWormPanel();

JFrame f =new JFrame();

f.setTitle("Worms");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(wp);

f.setSize(WIDTH, HEIGHT);

f.setVisible(true);

wp.startGame();

}

}

Somewhere in there is my problem.

What happens is that the right and bottom walls of the Rectangle are cut off of the screen because the window is not the right size. I changed the code to determine if the coordinates of the walls were accurate every iteration and they were. I also made it check the width and height every iteration by printing outthis.getWidth() andthis.getHeight()and found that instead of a600 x 400 window, I have a584 x 364. I also tried changing the panel and frame's size methods to make a new dimension instead of setting it directly from my constants and it had no effect. I also added directly to those constants to make it precisely 600 x 400 but the rectangle is still cut off, so maybe I also have a graphics issue. The only other potential issue I can think of is that I have Vista but I looked around here and searched google and found no similar issues to this.

I am not new to java but I am also not advanced. I just started using java again after about 6 months and I have made a pong game before without this problem, on another computer though.I am at my wits end. I'll check for responses tomorrow and thank you for any help or insight you can offer.

[5930 byte] By [gildedwinda] at [2007-11-27 9:18:19]
# 1

the problem is here

walls = new Rectangle(50, 50, WIDTH - 50, HEIGHT - 50);

at best the right/bottom walls will equal the frame's dimensions. try it as

walls = new Rectangle(50, 50, WIDTH - 100, HEIGHT - 100);

but this won't get you exactly what you want, due to the titlebar height (30?)

slightly different version

import javax.swing.*;

import java.awt.*;

class DemoWormPanel extends JPanel {

private static final int WIDTH = 600, HEIGHT = 400;

public DemoWormPanel() {

setBackground(Color.BLACK);

setPreferredSize(new Dimension(WIDTH-100,HEIGHT-100));

}

public static void main(String[] args) {

DemoWormPanel wp = new DemoWormPanel();

JFrame f = new JFrame();

f.setLayout(new GridBagLayout());

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.add(wp,new GridBagConstraints());

f.setSize(WIDTH, HEIGHT);

f.setVisible(true);

}

}

Michael_Dunna at 2007-7-12 22:09:28 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thank you very much. I had to look up GridBagLayout but I see that it is exactly what I needed.Thank you again.
gildedwinda at 2007-7-12 22:09:28 > top of Java-index,Desktop,Core GUI APIs...