0

I am trying to program a game, but I can't make exactly the big window I need. First println gives me 720 and second 732 and the window is really bigger than 720. How can I make the game display exactly 720 pixels high?

xCopterGame.java

import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.event.*;

public class xCopterGame extends JFrame implements MouseListener{

  private static final long serialVersionUID = 1L;

  Board panel=null;
  public xCopterGame(){
    panel = new Board();
    setTitle("Pong");
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    panel.setPreferredSize(new Dimension(1280,720));
    getContentPane().add(panel);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

    System.out.println(panel.getHeight());

    panel.yCopter = getHeight()/2-15;

    panel.addMouseListener(this);
    addMouseListener(this);
  }

  public void mousePressed(MouseEvent e) {
    panel.mousePressed(e);
  }

  public void mouseReleased(MouseEvent e) {
    panel.mouseReleased(e);
  }

  public static void main(String[] args) {
    new xCopterGame();
  }


  public void mouseClicked(MouseEvent e) {

  }
  public void mouseEntered(MouseEvent e) {

  }
  public void mouseExited(MouseEvent e) {

  }
}

Board.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

public class Board extends JPanel implements Runnable{
  private static final long serialVersionUID = 1L;

  Thread th=null;
  boolean mousePressed = false, firstClick = false;
  int yCopter;

  public Board(){
    th = new Thread(this);
    th.start();
  }

  public void paint(Graphics g){
    super.paint(g);
    this.setBackground(Color.black);
    g.setColor(Color.white);
    g.fillRect(80, yCopter, 30, 30);

    g.setColor(Color.gray);
    for(int i=0; i<37; i++){
      g.drawLine(0, i*20, this.getWidth(), i*20);
    }

    g.fillRect(100, 715, 30, 5);
    g.fillRect(100, 0, 30, 5);       
    System.out.println(this.getHeight());
  }

  public void run() {
    while(true){
      if(mousePressed == true)
        yCopter--;
      if(mousePressed == false && firstClick == true)
        yCopter++;

   //   repaint();

      try { 
        Thread.sleep(10); 
      } 
      catch(InterruptedException ex) {

      }
    }
  }

  public void mousePressed(MouseEvent e) {
    mousePressed = true;
    firstClick = true;
  }
  public void mouseReleased(MouseEvent e) {
    mousePressed = false;
  }
}
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
vtchom3
  • 23
  • 4
  • `Thread.sleep(10);` Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Mar 01 '13 at 00:52

2 Answers2

3

setResizable() may affect insets of the frame. It is best to call pack() after setResizable(). Try this order of calls:

setResizable(false);
pack();
setVisible(true);

For reference, see this analysis by @HovercraftFullOfEels

You may also find Should I avoid the use of set[Preferred|Maximum|Minimum]Size methods in Java Swing? helpful.

Community
  • 1
  • 1
tenorsax
  • 21,123
  • 9
  • 60
  • 107
0

I could notice you are just setting up the Board prefered size. If you want to force a specific size for your window, you can simply call the method JFrame.setSize() of you xCopterGame.

I encorage you to take a look at this link. There are other ways to implement a Swing UI without having to deal with this kind of problem when you use a LayoutManager.

Trein
  • 3,658
  • 27
  • 36
  • 1
    Windows have boarders and other content which is "squeezed" into the frames bounds, meaning the content space is then smaller the expected. What the OP is doing is actually correct. Set the preferred size of the content and let the frame add it's stuff to the outside, so the resulting frame size should be larger then the preferred size of the content. I think Aqua hit the nail on the head – MadProgrammer Feb 28 '13 at 23:31
  • Agree with @MadProgrammer - this is just going the wrong way. -1 for first paragraph, +1 for the second for referencing a Java Tutorial link re. layout managers. Net 0. – Andrew Thompson Mar 01 '13 at 00:50