Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

help: JPanel/JFrame display complexities, wrong size

843806Jun 30 2007 — edited Jun 30 2007
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.*;

public class DemoWormPanel extends JPanel implements Runnable{
    private static final int WIDTH = 600, HEIGHT = 400;
    private Graphics dbG;
    private Image dbImage;
    private Thread animator;
    private boolean running = false;
    private Rectangle walls;

    public DemoWormPanel() {
        super();
        setSize(WIDTH,HEIGHT);
    }

    public void 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);

    }

    public void 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);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(dbImage, 0, 0, this);
        g.dispose();
    }

    public void update(Graphics g){
        paint(g);
    }

    public void run() {
        while (running) {
            gameRender();
            repaint();

            try {
                Thread.sleep(1000/50);
            } catch (InterruptedException e) {}
        }
        System.exit(0);
    }

    public static void 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 out this.getWidth() and this.getHeight() and found that instead of a 600 x 400 window, I have a 584 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 28 2007
Added on Jun 30 2007
2 comments
578 views