Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Explain the Java pong game.

Asked by Puja Khatri Oct 13, 2022 1.5K views 2 answers
Share

About this question

 have recently written the following Pong game in Java:

Pong class:

import java.awt.Color;

import javax.swing.JFrame;


public class Pong extends JFrame {

    private final static int WIDTH = 700, HEIGHT = 450;

    private PongPanel panel;

  public Pong() {
        setSize(WIDTH, HEIGHT);
        setTitle("Pong");
        setBackground(Color.WHITE);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel = new PongPanel(this);
        add(panel);
    }
 public PongPanel getPanel() {
        return panel;
    }
public static void main(String[] args) {
        new Pong();
    }
}
PongPanel class:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;


import javax.swing.JPanel;

import javax.swing.Timer;


public class PongPanel extends JPanel implements ActionListener, KeyListener {
    private Pong game;
    private Ball ball;
    private Racket player1, player2;
    private int score1, score2;
    public PongPanel(Pong game) {
        setBackground(Color.WHITE);
        this.game = game;
        ball = new Ball(game);
        player1 = new Racket(game, KeyEvent.VK_UP, KeyEvent.VK_DOWN, game.getWidth() - 36);
        player2 = new Racket(game, KeyEvent.VK_W, KeyEvent.VK_S, 20);
        Timer timer = new Timer(5, this);
        timer.start();
        addKeyListener(this);
        setFocusable(true);
    }
    public Racket getPlayer(int playerNo) {
        if (playerNo == 1)
            return player1;
        else
            return player2;
    }
    public void increaseScore(int playerNo) {
        if (playerNo == 1)
            score1++;
        else
            score2++;
    }
    public int getScore(int playerNo) {
        if (playerNo == 1)
            return score1;
        else
            return score2;
    }
    private void update() {
        ball.update();
        player1.update();
        player2.update();
    }
    public void actionPerformed(ActionEvent e) {
        update();
        repaint();
    }
    public void keyPressed(KeyEvent e) {
        player1.pressed(e.getKeyCode());
        player2.pressed(e.getKeyCode());
    }
    public void keyReleased(KeyEvent e) {
        player1.released(e.getKeyCode());
        player2.released(e.getKeyCode());
    }
    public void keyTyped(KeyEvent e) {
        ;
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString(game.getPanel().getScore(1) + " : " + game.getPanel().getScore(2), game.getWidth() / 2, 10);
        ball.paint(g);
        player1.paint(g);
        player2.paint(g);
    }
}
Ball class:
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JOptionPane;
public class Ball {
    private static final int WIDTH = 30, HEIGHT = 30;
    private Pong game;
    private int x, y, xa = 2, ya = 2;
    public Ball(Pong game) {
        this.game = game;
        x = game.getWidth() / 2;
        y = game.getHeight() / 2;
    }
    public void update() {
        x += xa;
        y += ya;
        if (x < 0>            game.getPanel().increaseScore(1);
            x = game.getWidth() / 2;
            xa = -xa;
        }
        else if (x > game.getWidth() - WIDTH - 7) {
            game.getPanel().increaseScore(2);
            x = game.getWidth() / 2;
            xa = -xa;
        }
        else if (y < 0> game.getHeight() - HEIGHT - 29)
            ya = -ya;
        if (game.getPanel().getScore(1) == 10)
            JOptionPane.showMessageDialog(null, "Player 1 wins", "Pong", JOptionPane.PLAIN_MESSAGE);
        else if (game.getPanel().getScore(2) == 10)
            JOptionPane.showMessageDialog(null, "Player 2 wins", "Pong", JOptionPane.PLAIN_MESSAGE);
        checkCollision();
    }
    public void checkCollision() {
        if (game.getPanel().getPlayer(1).getBounds().intersects(getBounds()) || game.getPanel().getPlayer(2).getBounds().intersects(getBounds()))
            xa = -xa;
    }
    public Rectangle getBounds() {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }
    public void paint(Graphics g) {
        g.fillRect(x, y, WIDTH, HEIGHT);
    }
}
Racket class:
   public Rectangle getBounds() {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }
    public void paint(Graphics g) {
        g.fillRect(x, y, WIDTH, HEIGHT);
    }
}
Racket class:
import java.awt.Graphics;
import java.awt.Rectangle;
public class Racket {
    private static final int WIDTH = 10, HEIGHT = 60;
    private Pong game;
    private int up, down;
    private int x;
    private int y, ya;
    public Racket(Pong game, int up, int down, int x) {
        this.game = game;
        this.x = x;
        y = game.getHeight() / 2;
        this.up = up;
        this.down = down;
    }
    public void update() {
        if (y > 0 && y < game>            y += ya;
        else if (y == 0)
            y++;
        else if (y == game.getHeight() - HEIGHT - 29)
            y--;
    }
    public void pressed(int keyCode) {
        if (keyCode == up)
            ya = -1;
        else if (keyCode == down)
            ya = 1;
    }
    public void released(int keyCode) {
        if (keyCode == up || keyCode == down)
            ya = 0;
    }
    public Rectangle getBounds() {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }
    public void paint(Graphics g) {
        g.fillRect(x, y, WIDTH, HEIGHT);
    }
}
How can I improve/optimize my code?


Your answer

2 Answers

Angelnicaella Latest answer

Answered on Jan 12, 2026

Class structure is fine, but I’d start with small stuff: add @override to listener methods and rename width/height so you don’t hide fields from imageobserver. For players, enum is better than int, then increasescore for the second player reads unambiguously. Also, I’d move rendering out of ball/racket and leave them only state and physics, and let the panel draw based on the data. And move magic numbers 29 and 7 to constants, plus stop the timer and reset the score on win, otherwise the dialog pops up every tick. When I tried to figure out why swing timer calls repaint so often, I came across https://monopoly-live-game-in.com/ in the search results and it distracted me from debugging collisions in pong, so it’s better to keep the docs handy.

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.