Java 碰撞的球 MovingBall (整理)

简介: 1 package demo; 2 3 /** 4 * Java 碰撞的球 MovingBall (整理) 5 * 声明: 6 * 这份源代码没有注释,已经忘记了为什么要写他了,基本上应该是因为当时觉得好玩吧。
  1 package demo;
  2 
  3 /**
  4  *                     Java 碰撞的球 MovingBall (整理)
  5  * 声明:
  6  *     这份源代码没有注释,已经忘记了为什么要写他了,基本上应该是因为当时觉得好玩吧。
  7  * 有时候想想,也许是因为当时就是想做一个这样的效果的东西。
  8  *
  9  *                                   2016-1-2 深圳 南山平山村 曾剑锋
 10  */
 11 
 12 import java.awt.Color;
 13 import java.awt.Graphics;
 14 
 15 import javax.swing.JFrame;
 16 import javax.swing.JPanel;
 17 
 18 public class MovingBall extends JPanel {
 19 
 20     private static final long serialVersionUID = 1L;
 21     static int centerX = 600/2;
 22     static int centerY = 600/2;
 23     final Ball ball = new Ball(centerX, centerY);
 24     boolean cicleflag = true;
 25     int cicleSemi = 0;
 26     double angle = 0;
 27     int ballCicle = 30;
 28     public MovingBall() {
 29         start();
 30     }
 31     @Override
 32     public void paint(Graphics graphics) {
 33         super.paint(graphics);
 34         this.setBackground(Color.black);
 35         graphics.setColor(Color.BLUE);
 36         graphics.fillArc(centerX-cicleSemi/2, centerY-cicleSemi/2, cicleSemi, cicleSemi, 0, 360);
 37         ball.drawing(graphics);
 38         graphics.setColor(Color.white);
 39         graphics.drawArc(centerX-cicleSemi/2, centerY-cicleSemi/2, cicleSemi, cicleSemi, 0, 360);
 40         graphics.fillArc((int)(centerX+cicleSemi/2*Math.cos(angle*Math.PI/180)-ballCicle/2), (int)(centerY+cicleSemi/2*Math.sin(angle*Math.PI/180)-ballCicle/2), ballCicle, ballCicle, 0, 360);
 41         graphics.fillArc((int)(centerX+cicleSemi/2*Math.cos((angle+180)*Math.PI/180)-ballCicle/2), (int)(centerY+cicleSemi/2*Math.sin((angle+180)*Math.PI/180)-ballCicle/2), ballCicle, ballCicle, 0, 360);
 42     }
 43     public void start() {
 44         new Thread(new Runnable() {
 45             
 46             @Override
 47             public void run() {
 48                 while (true) {
 49                     try {
 50                         ballMove(ball);
 51                         
 52                         if (cicleflag == true) {
 53                             cicleSemi += 2;
 54                             if (cicleSemi > centerX) {
 55                                 cicleflag = false;
 56                             }
 57                         }else {
 58                             cicleSemi -= 2;
 59                             if (cicleSemi < 0) {
 60                                 cicleflag = true;
 61                             }
 62                         }
 63                         angle ++;
 64                         repaint();
 65                         Thread.sleep(20);
 66                     } catch (InterruptedException e) {
 67                         e.printStackTrace();
 68                     }
 69                 }
 70                 
 71             }
 72 
 73             public void ballMove(Ball ball) {
 74                 ball.x += ball.dx;
 75                 ball.y += ball.dy;
 76                 if (ball.x < 50 || ball.x > centerX*2-50) {
 77                     ball.dx = -ball.dx;
 78                 }else if (ball.y < 50 || ball.y > centerY*2-50) {
 79                     ball.dy = -ball.dy;
 80                 }
 81             }
 82         }).start();
 83     } 
 84     
 85     public static void main(String[] args) {
 86         JFrame jFrame = new JFrame();
 87         jFrame.setTitle("MovingBall");
 88         jFrame.setSize(centerX*2, centerY*2);
 89         jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 90         jFrame.setLocationRelativeTo(null);
 91         
 92         MovingBall moveBall = new MovingBall();
 93         jFrame.add(moveBall);
 94         jFrame.setVisible(true);
 95     }
 96 }
 97 
 98 class Ball{
 99     static int step = 10;
100     int x;
101     int y;
102     int dx;
103     int dy;
104     double angle;
105     int cicle = 30;
106     public Ball(int x, int y) {
107         this.x = x;
108         this.y = y;
109         this.angle = Math.random()*360;
110         if (angle <= 180) {
111             dx = (int) (step*Math.cos(angle*Math.PI/180));
112             dy = (int) (step*Math.sin(angle*Math.PI/180));
113         }else {
114             dx = (int) (step*Math.cos(angle*Math.PI/180));
115             dy = -(int) (step*Math.sin(angle*Math.PI/180));
116         }
117     }
118     public Ball(int x, int y, int angle) {
119         this.x = x;
120         this.y = y;
121         this.angle = angle;
122         if (angle <= 180) {
123             dx = (int) (step*Math.cos(angle*Math.PI/180));
124             dy = (int) (step*Math.sin(angle*Math.PI/180));
125         }else {
126             dx = (int) (step*Math.cos(angle*Math.PI/180));
127             dy = -(int) (step*Math.sin(angle*Math.PI/180));
128         };
129     }
130     public void drawing(Graphics graphics) {
131         graphics.setColor(Color.white);
132         graphics.fillArc(this.x-cicle/2, this.y-cicle/2, cicle, cicle, 0, 360);
133     }
134 }

效果如图:

目录
相关文章
|
3天前
|
云安全 人工智能 自然语言处理
|
7天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
683 17
|
10天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
739 57
Meta SAM3开源:让图像分割,听懂你的话
|
7天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
328 116
|
10天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
490 37
|
22天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
1天前
|
Rust 安全
掌握Rust文件读取(从零开始的IO操作指南)
本教程手把手教你用Rust读取文件,涵盖`read_to_string`一次性读取和`BufReader`逐行高效读取,适合初学者掌握安全、高效的Rust文件操作,助你轻松入门系统编程。
148 113