1

I have been working on making Pong in Java, but I can't seem to accurately set the size of the JFrame. Here is my main class:

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Pong extends JFrame {

    private final Game panel;
    private final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    public Pong() {
        setTitle("Pong");
        setSize(screenSize);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel = new Game(this);
        add(panel);
    }

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

}

When I run my program, the window takes up the whole screen, but when I try to get the dimensions of the screen, they are larger than those of the window, causing anything set at the lower extents of the window to get cut off. The only way I have found to get the true dimensions is to call this.getHeight(), this.getWidth(), or this.getSize() within the panel class. How can I get the true dimensions right off the bat?

Carter S.
  • 11
  • 4

4 Answers4

1

You could use JFrame#setExtendedState(JFrame.MAXIMIZED_BOTH)

f.setExtendedState(JFrame.MAXIMIZED_BOTH);

It has the same effect as when you enlarge a window in your OS

With this method, you can now have the "real" dimensions by calling the different methods mentionned above in the question.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

Try to call method setExtendedState.

public class Pong extends JFrame {

    private final Game panel;

    public Pong() {
        setTitle("Pong");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel = new Game(this);
        add(panel);
        setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
        //setVisible(true);
    }

    public static void main(String[] args) {
        Pong pong = new Pong();
        pong.setVisible(true);
    }

}
josivan
  • 1,963
  • 1
  • 15
  • 26
  • Using this method, the result looks the same. It seems as though it is including the menu bar of the window in the height. – Carter S. Mar 13 '16 at 18:08
  • @CarterS. Do you wanna the size of JFrame instead of desktop? I miss your point now. – josivan Mar 13 '16 at 19:07
  • In the context of Pong, my ball is going off the bottom of the screen before bouncing because I can't get the exact height of the window. `game.getHeight()` is about 15 pixels past the edge of the window. – Carter S. Mar 13 '16 at 21:44
  • DId you try to set the size of panel (Games) instead of JFrame (Pong)? – josivan Mar 13 '16 at 22:48
  • I'm now using `this.getSize()` within the panel class to work around the issue. Thanks for the help! – Carter S. Mar 14 '16 at 00:17
0
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Pong extends JFrame {

    private final Game panel;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();

    public Pong() {
        setTitle("Pong");
        setSize((int)width,(int)height);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel = new Game(this);
        add(panel);
    }

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

}

check this question How can I get screen resolution in java?

Community
  • 1
  • 1
A.K.
  • 2,284
  • 3
  • 26
  • 35
0

Put in your code:

this.setExtendedState(JFrame.MAXIMIZED_BOTH);

Details you can find in documentation.

Another questions on same subject is here.

Community
  • 1
  • 1
1ac0
  • 2,875
  • 3
  • 33
  • 47