Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:42:44+00:00 2026-06-09T13:42:44+00:00

I’m designing a simple ping pong game using Swing. The game is implemented by

  • 0

I’m designing a simple ping pong game using Swing. The game is implemented by 5 .java files – PingPongApp.java an entry point for JVM, MainPanel.java a panel that organizes 2 sub-panels: a button panel and a screen panel (ScreenPanel.java), then Ball.java and Racket.java which implement the ball and racket logic. I’m trying to draw both the ball and racket on the screen panel but the problem is that somehow the ball is the only object that is drawn when I run the app. Here’s my code:

Racket.java

import java.awt.*;
public class Racket{
    public Racket(int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    public void draw(Graphics g){
        g.setColor(Color.RED);
        g.fillRect(x, y, width, height);
    }
    private int x, y, width, height;
}

Ball.java

import java.awt.*;
public class Ball{
    public Ball(int x, int y, int velocityX, int velocityY){
        this.x = x;
        this.y = y;
        this.velocityX = velocityX;
        this.velocityY = velocityY;
    }
    public void setBounds(int width, int height){
        rightBoundary = width - DIAMETER;
        bottomBoundary = height - DIAMETER;
    }
    public void move(){
        // Move the ball
    }
    public void draw(Graphics g){
        g.fillOval(x, y, DIAMETER, DIAMETER)
    }
    private final static int DIAMETER = 21;
    private int x, y, velocityX, velocityY, rightBoundary, bottomBoundary;
}

ScreenPanel.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ScreenPanel extends JPanel{
    public ScreenPanel(){
        initComponents();
    }
    private void initComponents(){
        interval = 35;
        ball     = new Ball(130, 0, 2, 3);
        racket   = new Racket(120, 250, 70, 10);            
        setPreferredSize(new Dimension(200, 200));
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        timer = new Timer(interval, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt){
                ball.setBounds(getWidth(), getHeight());
                ball.move();
                repaint();  
            }
        });
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        ball.draw(g);
        racket.draw(g);  
    }
    public void startOrPauseGame(boolean turnOnOff){
        if(turnOnOff){
            timer.start();
        } else {
            timer.stop();
        }
    }
    private Racket racket;
    private Timer timer;
    private Ball ball;
    private int interval;
}

MainPanel.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainPanel extends JPanel{
    public MainPanel(){
        initComponents();
    }
    private void initComponents(){
        buttonPanel = new JPanel(new FlowLayout());
        screenPanel = new ScreenPanel();
        cmdStart    = new JButton("Start/Resume");
        cmdPause    = new JButton("Pause");
        addComponentsToPane();
    }
    public void addComponentsToPane(){
        buttonPanel.add(cmdStart);
        buttonPanel.add(cmdPause);
        setLayout(new BorderLayout());
        add(buttonPanel , BorderLayout.NORTH);
        add(screenPanel , BorderLayout.SOUTH);
        cmdStart.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt){
                // Start the game  
            }                   
        });
        cmdPause.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent evt){
                // Pause the game 
            }    
        });   
    }
    private ScreenPanel screenPanel;
    private JPanel buttonPanel;
    private JButton cmdStart, cmdPause;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:42:46+00:00Added an answer on June 9, 2026 at 1:42 pm

    The problem is that the y coordinate for your racket is greater then the preferred size height of the panel ScreenPanel:- 250 > 200:

        racket = new Racket(120, 250, 70, 10);
                                  ^
        setPreferredSize(new Dimension(200, 200));
    

    making the racket draw offscreen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm designing a game and thinking about using WPF for making a simple prototype
I'm designing a simple game, which uses Java 2D and Newtonian physics. Currently my
I am designing a simple Android game and I am having problem with collusion.
I'm designing a simple board game in jquery/php. The state of the board is
I just finished designing a simple code program. I called it a Guessing Game.
I am designing a simple application using c# and it has a web browser
I am designing a website which will query a simple MySQL database using AJAX
I am designing a simple C# WCF service using ASP.NET 4.0 and hosted on
I am in the process of designing a simple Java application which deals with
I'm designing a simple text editor using WxPython, and I want to put the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.