1

I am trying to create PingPong in java, however I am having troubles creating boundaries for the ball. After testing, I found that the JFrame opens too small; I set the width to 500 and the JFrame opens at 484 pixels. Why is this?

Here is my game class:

public class Game extends Canvas {

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

Display d;


public Game(){
    d = new Display(this);
    requestFocus();
}


public void start(){
    System.out.println("Game started.");
}
public void stop(){

}


public void paint(Graphics g){
    g.setColor(Color.DARK_GRAY);
    g.fillRect(0, 0, 500, 400);
    g.setColor(Color.GREEN);
    g.drawLine(483, 0, 483, 399);
    g.drawLine(0, 399, 499, 399);
}

}

Here is my JFrame class:

public class Display extends JFrame {

JFrame frame;
Dimension screenSize;

public Display(Game game){
    frame = new JFrame("PingPong");
    screenSize = new Dimension(500, 400);



    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(true);
    //frame.setSize(500, 400);
    frame.setPreferredSize(screenSize);
    frame.setMinimumSize(screenSize);
    frame.setMaximumSize(screenSize);

    frame.setLocationRelativeTo(null);
    frame.add(game);
    //frame.pack();
    game.start();

}

}

Lol
  • 13
  • 1
  • 3

2 Answers2

3

When you call setSize on a JFrame, it includes the size of the title bar, as well as any other decoration around the window. So the actual drawable area you have is going to be a bit less than that. You can work around this by setting your Game's preferred size, and using pack on the JFrame.

So if you change your code to this:

public Display(Game game){
    super("PingPong");
    screenSize = new Dimension(500, 400);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true);
    game.setPreferredSize(screenSize);

    setLocationRelativeTo(null);
    add(game);
    pack();
    setVisible(true);
    game.start();
}

It will work. This tells your actual game what size it wants to be (500x400), and then has the frame resize so that it can fully contain it. That will give you 500x400 pixels to work with in your actual drawing.

Notice that I removed all the frame references. Since Display itself is already a JFrame you don't need to create another one. Just use the Display object itself.

resueman
  • 10,572
  • 10
  • 31
  • 45
  • How exactly would I do this? I am fairly new to game development in java – Lol Feb 02 '16 at 22:23
  • @Lol: Take a look at this [Stackoverflow answer](http://stackoverflow.com/a/35002727/300257). It's not ping-pong, but it shows you how to draw properly in Java Swing. Also, take a look at Oracle's [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) to see another example. – Gilbert Le Blanc Feb 02 '16 at 22:35
  • @Lol It's pretty easy. Looking at your code, I see that you actually had it pretty close. You just need to change `frame.setPreferredSize(screenSize)` to `game.setPreferredSize(screenSize)`, comment out the `setMinimumSize` and `setMaximumSize` lines, and uncomment `frame.pack()`. I'll add what it will look like to my answer. – resueman Feb 02 '16 at 23:12
0

Just delete frame.pack() then it will work.

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • 1
    Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Tomer Shetah Dec 19 '20 at 08:29