如何创建一个显示多个弹跳球而不重叠的 Java 应用程序?
Java 弹跳球
在此示例中,我们将创建一个 Java 应用程序,在屏幕上绘制多个从框架边缘弹起的球。
问题
当尝试绘制多个球时,它们会相互覆盖,因为它们被添加到相同位置。
解决方案
要解决此问题,我们需要:
- 创建球列表:我们将使用一个 ArrayList 来存储球对象。
- 将球添加到内容中窗格: 我们不会将球对象直接添加到内容窗格,而是将它们添加到列表中。
- 绘制球: 我们将迭代列表并绘制每个球都在其指定位置。
- 处理运动:每个球都有自己的线程来处理其运动,确保它们不会互相覆盖。
这是实现这些更改的修改后的代码:
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; for (Ball ball : balls) { ball.paint(g2); } } public void run() { while (isVisible()) { try { Thread.sleep(delay); } catch (InterruptedException e) { System.out.println("interrupted"); } move(); repaint(); } } public void move() { for (Ball ball : balls) { int newX = ball.x + ball.vx; int newY = ball.y + ball.vy; if(newX + ball.diameter > getWidth()) { ball.vx *= -1; } if(newY + ball.diameter > getHeight()) { ball.vy *= -1; } if(newX < 0) { ball.vx *= -1; } if(newY < 0) { ball.vy *= -1; } ball.x = newX; ball.y = newY; } } 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); ball1.balls.add(ball1); ball2.balls.add(ball2); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); ball1.start(); ball2.start(); } }
在此更新的解决方案中:
- 我们创建单独的 Ball 对象并将它们添加到列表中。
- 我们迭代paintComponent 中的列表来绘制所有
- 每个球都有自己的运动线程,防止覆盖。
- 我们还实现了一个 start 方法来确保线程在帧可见后启动。
按照以下步骤,我们就可以创建一个程序,成功在屏幕上绘制多个弹跳球。
以上是如何创建一个显示多个弹跳球而不重叠的 Java 应用程序?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

在使用TKMyBatis进行数据库查询时,如何优雅地获取实体类变量名以构建查询条件,是一个常见的难题。本文将针...
