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 7568333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:53:56+00:00 2026-05-30T14:53:56+00:00

I am having a problem getting my application to draw a Ball when i

  • 0

I am having a problem getting my application to draw a Ball when i click on the frame.
I thought my code was correct, and I don’t get any errors, but it still doesn’t work.
I feel the problem is with the MouseListener implementation and that the application is not handling the MouseEvent properly.

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class BouncingBallApp extends JFrame
{

public static void main(String[] args)
{
 Container container;
 BouncingBallApp bouncingBalls = new BouncingBallApp();
 bouncingBalls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 container = new Container();
 BouncingBallPanel BBP = new BouncingBallPanel();
 container.add(BBP);
 bouncingBalls.addMouseListener(BBP);
 //addMouseListener;
 bouncingBalls.setBackground( Color.WHITE );
 bouncingBalls.setSize(400,300);
 bouncingBalls.setVisible( true );



}//end of main method
}//end of BouncingBallApp



class BouncingBallPanel extends JPanel implements MouseListener, Runnable
{
private Ball[] ballArray = new Ball[20];
private int ballCount = 0;
public void run()
{

for(int i = 0; i<ballArray.length; i++){
if(ballArray[i] != null){
ballArray[i].move();}} 
repaint(); 
//delay(1);

}
public void mouseClicked(MouseEvent e)
{


ballArray[ballCount] = new Ball();
ballCount++;
if(ballCount == 1)
(new Thread(new BouncingBallPanel())).start();

}

//empty interface methods
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}

public void paintComponent(Graphics g)
{
super.paintComponent( g );
Graphics2D g2d = (Graphics2D) g;

for(int i = 0; i<ballArray.length; i++)
{
if(ballArray[i] != null){
g2d.setColor(ballArray[i].getColor());
g2d.fill(new Ellipse2D.Double(ballArray[i].getX(),ballArray[i].getY(),ballArray   
[i].getDiameter(),ballArray[i].getDiameter()));}
}//end of for loop

}

}//end of BouncingBallPanel 

class Ball
{
private double x;
private double y;
private double deltaX;
private double deltaY;
private double diameter;
private Color color;

Random random = new Random();

public Ball()
{
    x = random.nextInt(400);
y = random.nextInt(300);
deltaX = 2;
deltaY = 2;
diameter = 10;
color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
}//end of constructor

public double getX(){
return x;}

public double getY(){
return y;}

public double getDiameter(){
return diameter;}

public Color getColor(){
return color;}

public void move()
{
x += deltaX;
y += deltaY;

if (x < 0) {  
x = 0; 
deltaX = -deltaX;}

else if (x > 400) { 
x = 400;    
deltaX = -deltaX;}

if (y < 0) {     
y = 0;
deltaY = -deltaY;}

else if (y > 300) { 
y = 300;
deltaY = -deltaY;}


}//end of method move

}//end of ball
  • 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-05-30T14:53:57+00:00Added an answer on May 30, 2026 at 2:53 pm

    3 Things

    1. No size for container
    2. You are not adding container
    3. No loop to update graphics

    The code below is working

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class BouncingBallApp extends JFrame {
    
        public static void main(String[] args) {
            // Container container;
            BouncingBallApp bouncingBalls = new BouncingBallApp();
            bouncingBalls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // container = new Container();
            BouncingBallPanel BBP = new BouncingBallPanel();
            // container.add(BBP);
            bouncingBalls.addMouseListener(BBP);
            // addMouseListener;
            bouncingBalls.setBackground(Color.WHITE);
            bouncingBalls.setSize(400, 300);
    
            BBP.setSize(400, 300);
            BBP.setLayout(null);
            bouncingBalls.setContentPane(BBP);
    
            /*
             * JTextField jtf = new JTextField(); BBP.add(jtf); jtf.setSize(100,
             * 20);
             */
    
            bouncingBalls.setVisible(true);
    
        }// end of main method
    }// end of BouncingBallApp
    
    class BouncingBallPanel extends JPanel implements MouseListener {
        private Ball[] ballArray = new Ball[20];
        private int ballCount = 0;
    
        public void mouseClicked(MouseEvent e) {
    
            ballArray[ballCount] = new Ball();
            ballCount++;
            if (ballCount == 1) {
    
                final Runnable updateGraphics = new Runnable() {
                    public void run() {
                        for (int i = 0; i < ballArray.length; i++) {
                            if (ballArray[i] != null) {
                                ballArray[i].move();
                            }
                        }
                        repaint();
                    }
                };
    
                Runnable animation = new Runnable() {
                    public void run() {
                        while (true) {
                            try {
                                EventQueue.invokeLater(updateGraphics);
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                return;
                            }
                        }
                    }
                };
    
                new Thread(animation).start();
    
            }
    
        }
    
        // empty interface methods
        public void mouseExited(MouseEvent e) {
        }
    
        public void mouseReleased(MouseEvent e) {
        }
    
        public void mouseEntered(MouseEvent e) {
        }
    
        public void mousePressed(MouseEvent e) {
    
        }
    
        public void paintComponent(Graphics g) {
            // super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
    
            for (int i = 0; i < ballArray.length; i++) { if (ballArray[i] !=
                null) { System.out.println(" ball " + i + " , " +
                        ballArray[i].getColor() + " , " + ballArray[i].getX() + " , " +
                        ballArray[i].getY() + " , " + ballArray[i].getDiameter());
                g2d.setColor(ballArray[i].getColor());
                g2d.fillRect((int)ballArray[i].getX(), (int)ballArray[i].getY(),
                        (int)ballArray[i].getDiameter(), (int)ballArray[i].getDiameter()); }
            }// end of for loop
    
    
        }
    
    }// end of BouncingBallPanel
    
    class Ball {
        private double x;
        private double y;
        private double deltaX;
        private double deltaY;
        private double diameter;
        private Color color;
    
        Random random = new Random();
    
        public Ball() {
            x = random.nextInt(400);
            y = random.nextInt(300);
            deltaX = 2;
            deltaY = 2;
            diameter = 10;
            color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        }// end of constructor
    
        public double getX() {
            return x;
        }
    
        public double getY() {
            return y;
        }
    
        public double getDiameter() {
            return diameter;
        }
    
        public Color getColor() {
            return color;
        }
    
        public void move() {
            x += deltaX;
            y += deltaY;
    
            if (x < 0) {
                x = 0;
                deltaX = -deltaX;
            }
    
            else if (x > 400) {
                x = 400;
                deltaX = -deltaX;
            }
    
            if (y < 0) {
                y = 0;
                deltaY = -deltaY;
            }
    
            else if (y > 300) {
                y = 300;
                deltaY = -deltaY;
            }
    
        }// end of method move
    
    }// end of ball
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a problem getting a TreeView control to display node images. The code below
I'm having a problem getting a change event to register with the following code:
i am having a problem with my application,.i have this code in my mainviewcontroller
I am having problem getting my tooltip to work when using the jQuery load()
I am having a problem getting a list of fields from a query defined
I'm having a problem getting access to a database which lives on a remote
I am having a problem getting my project to link with the PhysX libraries
I'm having a problem getting a simple ASP.NET webpage to display. The page contains
I'm having a problem getting XCode to deal with a particular file structure that
I am having a problem getting WinDbg to use the PDB files for my

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.