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

  • Home
  • SEARCH
  • 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 8842381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:56:38+00:00 2026-06-14T10:56:38+00:00

Im newish to java, and im writing a pong game. Ive got the ball

  • 0

Im newish to java, and im writing a pong game.
Ive got the ball to move (Thats my first priority) and make it apear on screen.
But for some Reason its leaving a black line behind it, I dont know if im supposed to errase this or something, but heres the code of my 2 classes (The Ball Class is just a class to save the info of the ball)

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

public class Main extends Canvas implements Runnable{
    private static final long serialVersionUID = 1L;

    public static int Width=650;
    public static int Height=600;
    public boolean Running=false;
    public Thread thread;
    public Ball ball = new Ball();

    public static void main(String[] args){
      Main game = new Main();
      JFrame frame = new JFrame();
      frame.setSize(Width+25, Height+49);
      frame.setTitle("Pong By Poo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
      frame.add(game);
      game.start();
    }


    public void start(){
      if(Running==true){
        return;
      } else {
        Running=true;
        thread = new Thread(this);
        thread.start();
      }
    }

    public void run(){
      while(Running==true){
        try{
          Thread.sleep(5);
          Draw();
          Update();
          ball.YVelocity();
          ball.XVelocity();
        } catch(Exception e){}
      }
    }

    public void Draw(){
      BufferStrategy bs = this.getBufferStrategy();
      if(bs==null){
        createBufferStrategy(2);
      } else {
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillOval(ball.BallLocationX, ball.BallLocationY, 20, 20);
        g.dispose();
        bs.show();
      }
    }

    public void Update(){
      if(ball.BallLocationX==0) {
        ball.BallMovementX=true;
        System.out.println("Ball has hit the Left");
      }
      if(ball.BallLocationX==Width) {
        ball.BallMovementX=false;
        System.out.println("Ball has hit the Right");
      }
      if(ball.BallLocationY==0){
        ball.BallMovementY=true;
        System.out.println("Ball has hit the Top");
      }
      if(ball.BallLocationY==Height){
        ball.BallMovementY=false;
        System.out.println("Ball has hit the bottom");
      }
    }
  }



import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Ball extends JPanel{
  public int BallLocationX;
  public int BallLocationY;
  public boolean BallMovementY; //true makes the ball go up, false makes it go down
  public boolean BallMovementX; //True makes the ball go right, false makes it go left.

  public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillOval(BallLocationX, BallLocationY, 10, 10);
  }

  //moves the ball left to right
  public int YVelocity(){
    if(BallMovementY==true){
      BallLocationY++;
    } else {
      BallLocationY--;
    }

    return BallLocationY;
  }

  //Moves the ball up and down
  public int XVelocity(){
    if(BallMovementX==true){
      BallLocationX+=2;
    } else {
      BallLocationX-=2;
    }

    return BallLocationX;
    }
}

Please help!

  • 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-14T10:56:39+00:00Added an answer on June 14, 2026 at 10:56 am

    Essentially, everywhere your ball moves stays permanently Color.BLACK on your Canvas. If you want to get rid of that, you need to refresh every time the ball moves and repaint the Canvas to Color.WHITE (or whatever) before painting the ball’s position again.

    Specifically, look at your code here:

    public void Draw(){
      BufferStrategy bs = this.getBufferStrategy();
      if(bs==null){
        createBufferStrategy(2);
      } else {
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillOval(ball.BallLocationX, ball.BallLocationY, 20, 20);
        g.dispose();
        bs.show();
      }
    }
    

    There is no logic here that overwrites your previous changes to the Canvas indicating the ball’s location.

    Also, as a side nitpick, Java standards are to have method names in camelCase.

    And to answer the question in the comments: there is nothing in the Canvas API which automatically understands what you want the default background of the Canvas to be and can reset all other graphics attributes to that. To get this functionality, however, all you’ll need to do is repaint your default layout (whether it’s all one color, or a basic background image, or anything else) before painting the ball’s position on top of it.

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

Sidebar

Related Questions

I'm newish to Java and am trying to do somethings with dates. First I
I'm extremly newish to Mac (now is the first time I see this operating
Im a newish Ruby/Rails developer with years of Java experience. This security question is
I'm newish to TFS and am working with VS and TFS 2010 RC releases.
I am newish to Ruby and I am trying to write a method to
I'm newish to Python (and stackoverflow)...bear with me! I have a dictionary that looks
Is there a default value for Twitter's newish max_id parameter? I've tried: https://api.twitter.com/1/statuses/user_timeline.json?screen_name=justinbieber&trim_user=true&count=200&max_id=false https://api.twitter.com/1/statuses/user_timeline.json?screen_name=justinbieber&trim_user=true&count=200&max_id=null
Magento is a newish (past 5 years) PHP based Ecommerce system with an architecture
I'm newish to the python ecosystem, and have a question about module editing. I
I am newish to MVC and understand all the great things about it, including

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.