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();
}
}