Java 碰撞的球 MovingBall (整理)
package demo; /**
* Java 碰撞的球 MovingBall (整理)
* 声明:
* 这份源代码没有注释,已经忘记了为什么要写他了,基本上应该是因为当时觉得好玩吧。
* 有时候想想,也许是因为当时就是想做一个这样的效果的东西。
*
* 2016-1-2 深圳 南山平山村 曾剑锋
*/ import java.awt.Color;
import java.awt.Graphics; import javax.swing.JFrame;
import javax.swing.JPanel; public class MovingBall extends JPanel { private static final long serialVersionUID = 1L;
static int centerX = 600/2;
static int centerY = 600/2;
final Ball ball = new Ball(centerX, centerY);
boolean cicleflag = true;
int cicleSemi = 0;
double angle = 0;
int ballCicle = 30;
public MovingBall() {
start();
}
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
this.setBackground(Color.black);
graphics.setColor(Color.BLUE);
graphics.fillArc(centerX-cicleSemi/2, centerY-cicleSemi/2, cicleSemi, cicleSemi, 0, 360);
ball.drawing(graphics);
graphics.setColor(Color.white);
graphics.drawArc(centerX-cicleSemi/2, centerY-cicleSemi/2, cicleSemi, cicleSemi, 0, 360);
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);
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);
}
public void start() {
new Thread(new Runnable() { @Override
public void run() {
while (true) {
try {
ballMove(ball); if (cicleflag == true) {
cicleSemi += 2;
if (cicleSemi > centerX) {
cicleflag = false;
}
}else {
cicleSemi -= 2;
if (cicleSemi < 0) {
cicleflag = true;
}
}
angle ++;
repaint();
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
} } public void ballMove(Ball ball) {
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.x < 50 || ball.x > centerX*2-50) {
ball.dx = -ball.dx;
}else if (ball.y < 50 || ball.y > centerY*2-50) {
ball.dy = -ball.dy;
}
}
}).start();
} public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setTitle("MovingBall");
jFrame.setSize(centerX*2, centerY*2);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLocationRelativeTo(null); MovingBall moveBall = new MovingBall();
jFrame.add(moveBall);
jFrame.setVisible(true);
}
} class Ball{
static int step = 10;
int x;
int y;
int dx;
int dy;
double angle;
int cicle = 30;
public Ball(int x, int y) {
this.x = x;
this.y = y;
this.angle = Math.random()*360;
if (angle <= 180) {
dx = (int) (step*Math.cos(angle*Math.PI/180));
dy = (int) (step*Math.sin(angle*Math.PI/180));
}else {
dx = (int) (step*Math.cos(angle*Math.PI/180));
dy = -(int) (step*Math.sin(angle*Math.PI/180));
}
}
public Ball(int x, int y, int angle) {
this.x = x;
this.y = y;
this.angle = angle;
if (angle <= 180) {
dx = (int) (step*Math.cos(angle*Math.PI/180));
dy = (int) (step*Math.sin(angle*Math.PI/180));
}else {
dx = (int) (step*Math.cos(angle*Math.PI/180));
dy = -(int) (step*Math.sin(angle*Math.PI/180));
};
}
public void drawing(Graphics graphics) {
graphics.setColor(Color.white);
graphics.fillArc(this.x-cicle/2, this.y-cicle/2, cicle, cicle, 0, 360);
}
}
效果如图:
Java 碰撞的球 MovingBall (整理)的更多相关文章
- JAVA中去掉空格经典整理
JAVA中去掉空格经典整理 JAVA中去掉空格 1. String.trim() --------------trim()是去掉首尾空格 2.str.replac ...
- Java RGB数组图像合成 ImageCombining (整理)
/** * Java RGB数组图像合成 ImageCombinning (整理) * * 2016-1-2 深圳 南山平山村 曾剑锋 * * 注意事项: * 1.本程序为java程序,同时感谢您花费 ...
- Java 炫舞按键功能 DancingPlay (整理)
/** * Java 炫舞按键功能 DancingPlay (整理) * 2016-1-2 深圳 南山平山村 曾剑锋 * * 设计声明: * 1.本次设计是模仿QQ炫舞类游戏,当图标到红色的检测区域时 ...
- 近5年常考Java面试题及答案整理(三)
上一篇:近5年常考Java面试题及答案整理(二) 68.Java中如何实现序列化,有什么意义? 答:序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写 ...
- 近5年常考Java面试题及答案整理(二)
上一篇:近5年常考Java面试题及答案整理(一) 31.String s = new String("xyz");创建了几个字符串对象? 答:两个对象,一个是静态区的"x ...
- Java 面试/笔试题神整理 [Java web and android]
Java 面试/笔试题神整理 一.Java web 相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并 ...
- java 开发面试题小整理(一)
本篇文档将持续更新,有基础滴,也有深层次的,谢谢! 1.看下面的程序是否有问题,如果有问题,请指出并说明理由. * byte b1 = 3; * byte b2 = 4; * byte b3 = b1 ...
- Elasticsearch Java API 很全的整理
Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClient API两种.相比来说transportClient API效率更高, ...
- Java 5-11新特性的整理(转)
Java 5-11新特性的整理(转) 作者:拔剑少年 简书地址:https://www.jianshu.com/u/dad4d9675892博客地址:https://it18monkey.github ...
随机推荐
- 有了 Docker,用 JavaScript 框架开发的 Web 站点也能很好地支持网络爬虫的内容抓取
点这里 阅读目录 用 AngularJS(以及其它 JavaScript 框架)开发的 Web 站点不支持爬虫的抓取 解决方案 为什么公开我们的解决方案 实现 AngularJS 服务 结论 Pr ...
- 来认识下less css
简介 感觉自己都out了,跟不上web时代的潮流了,前不久才刚接触这玩意,发觉lesscss在某些方面还挺有用的,说白了这东西就是一种动态的样式语言,语法类似于css,可以像java那样进行编译,生成 ...
- 免安装jdk 和 免安装tomcat
免安装tomcat 运行的时候要执行免安装的 jdk,可以进行如下设置. 在 startup.bat 里加上这么一句, set "JAVA_HOME=C:\jdk1.6.0_43"
- 由CAST()函数在.NET1.1和.NET4.0下处理机制不同所引发的BUG
.NET 1.1版本下使用日期强制转换函数,比如:"select cast(ActionDate as char(7)) as ActionDate from ST_BookAction ...
- WCF传输Dataset大数据量 -压缩
由于WCF不能传输DataTable(不能序列化),所以更多项目中都会使用DataSet作为查询集合的首选返回类型,但是由于DataSet会生成很多的状态信息等,所以DataSet体积也会变大,有几种 ...
- 浅说Java中的反射机制(二)
写过一篇Java中的反射机制,不算是写,应该是抄了,因为那是别人写的,这一篇也是别人写的,摘抄如下: 引自于Java基础--反射机制的知识点梳理,作者醉眼识朦胧.(()为我手记) 什么是反射? 正常编 ...
- lintcode:最长公共子序列
题目 最长公共子序列 给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度. 样例 给出"ABCD" 和 "EDCA",这个LCS是 "A& ...
- Android核心分析 之十Android GWES之基本原理篇
Android GWES基本框架篇 我这里的GWES这个术语实际上从Microsoft 的Window上移植过来的,用GWES来表示Android的窗口事件系统不是那么准确,在Android中Wind ...
- CSS3伪类选择器
first-line 设置首行样式 first-letter 设置首字母样式 before 在某元素前插入内容并设置内容样式 after 在某元素后插入内容并设置内容样式 <!DOCTYP ...
- js浮点数的计算
js在计算浮点数时可能不够准确,会产生舍入误差的问题,这是使用基于IEEE745数值的浮点计算的通病,并非ECMAScript一家,其他使用相同数值格式的语言也存在这个问题. 这里讲一 ...