Moving Ball Using Thread in Java Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Prerequisites: Java.lang.Thread class in JavaMouseListener and MouseMotionListener in Java This is a simple Java code including the concept of Thread and Java AWT to implement three balls moving in a particular path in an AWT frame. In this implementation, three balls are taken in an AWT frame and have specified their paths within the frame using some if conditions. For the movement of balls concept of Thread is used. Here a Thread is created in the constructor of the class and it will start when the mouse is clicked on the frame(Implemented using Mouse Listener). To make balls look like moving sleep method of thread is used. It will stop the balls moving for a few milliseconds and then start again so it will look like the balls are moving. What we are going to build in this article? A sample video is given below to get an idea about what we are going to do in this article. Implementation Java import java.awt.*; import java.awt.event.*; class Bouncing_Balls extends Frame implements MouseListener { // initializing co-ordinates int x = 40, y = 40, t1 = 1, t2 = 1; int x1 = 200, y1 = 40, t12 = 1, t22 = 1; int x2 = 100, y2 = 100, t13 = 1, t23 = 1; Thread th; // Making constructor Bouncing_Balls() { setSize(700, 800); setVisible(true); // Creating a new Thread th = new Thread(new Thread() { public void run() { while (true) { x = x + t1; y = y + t2; x1 = x1 + t12; y1 = y1 + t22; x2 = x2 - t13; y2 = y2 - t23; // specifying some condition to make // balls move in a particular path if (x < 0 || x > 680) t1 = t1 * (-1); if (y < 20 || y > 780) t2 = t2 * (-1); if (x1 < 0 || x1 > 680) t12 = t12 * (-1); if (y1 < 20 || y1 > 780) t22 = t22 * (-1); if (x2 < 0 || x2 > 680) t13 = t13 * (-1); if (y2 < 20 || y2 > 780) t23 = t23 * (-1); try { // Calling sleep method this.sleep(5); } catch (Exception E) { } repaint(); } } }); addMouseListener(this); } public void mouseClicked(MouseEvent M) { // Thread will start when mouse is clicked th.start(); } public void mousePressed(MouseEvent M) {} public void mouseReleased(MouseEvent M) {} public void mouseEntered(MouseEvent M) {} public void mouseExited(MouseEvent M) {} public void paint(Graphics g) { g.setColor(Color.pink); g.fillOval(x, y, 40, 40); g.setColor(Color.pink); g.fillOval(x1, y1, 40, 40); g.setColor(Color.pink); g.fillOval(x2, y2, 40, 40); } public static void main(String[] args) { Bouncing_Balls B = new Bouncing_Balls(); } } Output Comment More infoAdvertise with us Next Article Moving Ball Using Thread in Java D dhruvishavaghani Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads How to Solve Deadlock using Threads in Java? If two threads are waiting for each other forever such type of infinite waiting is called deadlock in java. Synchronized keyword is the only reason for deadlock situation hence while using synchronized keyword we have to take special care. There is no resolution technique for deadlock, but several p 6 min read How to Copy a File in Multiple Threads Using Java? Copying files is a task, in programming that deals with file manipulation. Although Java offers tools, for file handling copying files can slow down our application's performance. To optimize this process, we can take advantage of multithreading, which enables threads to work on various sections of 3 min read Java Threading Programs - Basic to Advanced Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing paral 3 min read How to Monitor a Thread's Status in Java? The Java language support thread synchronization through the use of monitors. A monitor is associated with a specific data item and functions as a lock on that data. When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data. In order to mo 3 min read How to Temporarily Stop a Thread in Java? The suspend() method of thread class puts the thread from running to waiting state. This method is employed if you would like to prevent the thread execution and begin it again when a particular event occurs. This method allows a thread to temporarily cease execution. The suspended thread is often r 2 min read Producer Consumer Solution using BlockingQueue in Java Thread The Producer-Consumer problem is a synchronization issue that arises when one or more threads generate data, placing it on a buffer, and simultaneously, one or more threads consume data from the same buffer. Doing so can cause a race condition in which the threads are racing against each other to c 6 min read Java Program to Create a Thread Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. The slave thread is created as a result of the main thread. This is the 4 min read Java Program to Use Exceptions with Thread Exceptions are the events that occur due to the programmer error or machine error which causes a disturbance in the normal flow of execution of the program. When a method encounters an abnormal condition that it can not handle, an exception is thrown as an exception statement. Exceptions are caught 5 min read How to Use Locks in Multi-Threaded Java Program? A lock may be a more flexible and complicated thread synchronization mechanism than the standard synchronized block. A lock may be a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: just one thread at a time can ac 6 min read How to make ArrayList Thread-Safe in Java? In Java, Thread is the smallest unit of execution within the program. It represents an independent path of execution that can run concurrently with other threads. When dealing with multi-threaded applications, where multiple threads are accessing and modifying data concurrently, it's crucial to ensu 3 min read Like