


How can I create multiple bouncing balls in a Java application using a list to manage them?
Multiple Bouncing Balls in Java
Drawing multiple bouncing balls on a Java application can be achieved by using a list to store the balls and iterating through it to move and draw each ball. Here's a modified version of your code:
import java.awt.*; import javax.swing.*; import java.util.ArrayList; import java.util.List; public class Ball extends JPanel implements Runnable { List<Ball> balls = new ArrayList<Ball>(); Color color; int diameter; long delay; private int x; private int y; private int vx; private int vy; public Ball(String ballcolor, int xvelocity, int yvelocity) { if(ballcolor == "red") { color = Color.red; } else if(ballcolor == "blue") { color = Color.blue; } else if(ballcolor == "black") { color = Color.black; } else if(ballcolor == "cyan") { color = Color.cyan; } else if(ballcolor == "darkGray") { color = Color.darkGray; } else if(ballcolor == "gray") { color = Color.gray; } else if(ballcolor == "green") { color = Color.green; } else if(ballcolor == "yellow") { color = Color.yellow; } else if(ballcolor == "lightGray") { color = Color.lightGray; } else if(ballcolor == "magenta") { color = Color.magenta; } else if(ballcolor == "orange") { color = Color.orange; } else if(ballcolor == "pink") { color = Color.pink; } else if(ballcolor == "white") { color = Color.white; } diameter = 30; delay = 40; x = 1; y = 1; vx = xvelocity; vy = yvelocity; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (Ball ball : balls) { g.setColor(ball.color); g.fillOval(ball.x, ball.y, 30, 30); //adds color to circle g.setColor(Color.black); g2.drawOval(ball.x, ball.y, 30, 30); //draws circle } } public void run() { while(isVisible()) { try { Thread.sleep(delay); } catch(InterruptedException e) { System.out.println("interrupted"); } for (Ball ball : balls) { ball.move(); } repaint(); } } public void move() { if(x + vx < 0 || x + diameter + vx > getWidth()) { vx *= -1; } if(y + vy < 0 || y + diameter + vy > getHeight()) { vy *= -1; } x += vx; y += vy; } private void start() { while(!isVisible()) { try { Thread.sleep(25); } catch(InterruptedException e) { System.exit(1); } } Thread thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String[] args) { Ball ball1 = new Ball("red",3,2); Ball ball2 = new Ball("blue",6,2); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(ball1); f.getContentPane().add(ball2); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); ball1.start(); ball2.start(); } }
In this modified code:
- The balls list is used to store instances of the Ball class.
- The paintComponent method iterates through the balls list and draws each ball on the panel.
- The run method also iterates through the balls list and moves each ball before repainting the panel.
Benefits of Using a List:
- It allows you to add and remove balls dynamically.
- You can easily control the number of balls in the application.
- The list manages the references to each ball, simplifying memory management.
The above is the detailed content of How can I create multiple bouncing balls in a Java application using a list to manage them?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Start Spring using IntelliJIDEAUltimate version...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
