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 (整理)的更多相关文章

  1. JAVA中去掉空格经典整理

    JAVA中去掉空格经典整理 JAVA中去掉空格          1. String.trim() --------------trim()是去掉首尾空格           2.str.replac ...

  2. Java RGB数组图像合成 ImageCombining (整理)

    /** * Java RGB数组图像合成 ImageCombinning (整理) * * 2016-1-2 深圳 南山平山村 曾剑锋 * * 注意事项: * 1.本程序为java程序,同时感谢您花费 ...

  3. Java 炫舞按键功能 DancingPlay (整理)

    /** * Java 炫舞按键功能 DancingPlay (整理) * 2016-1-2 深圳 南山平山村 曾剑锋 * * 设计声明: * 1.本次设计是模仿QQ炫舞类游戏,当图标到红色的检测区域时 ...

  4. 近5年常考Java面试题及答案整理(三)

    上一篇:近5年常考Java面试题及答案整理(二) 68.Java中如何实现序列化,有什么意义? 答:序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写 ...

  5. 近5年常考Java面试题及答案整理(二)

    上一篇:近5年常考Java面试题及答案整理(一) 31.String s = new String("xyz");创建了几个字符串对象? 答:两个对象,一个是静态区的"x ...

  6. Java 面试/笔试题神整理 [Java web and android]

    Java 面试/笔试题神整理 一.Java web 相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并 ...

  7. java 开发面试题小整理(一)

    本篇文档将持续更新,有基础滴,也有深层次的,谢谢! 1.看下面的程序是否有问题,如果有问题,请指出并说明理由. * byte b1 = 3; * byte b2 = 4; * byte b3 = b1 ...

  8. Elasticsearch Java API 很全的整理

    Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClient API两种.相比来说transportClient API效率更高, ...

  9. Java 5-11新特性的整理(转)

    Java 5-11新特性的整理(转) 作者:拔剑少年 简书地址:https://www.jianshu.com/u/dad4d9675892博客地址:https://it18monkey.github ...

随机推荐

  1. DELPHI 获取本月 的第一天 和 最后一天

    USER :DateUtils 使用 StartOfTheMonth 和 EndOfTheMonth 函数获取即可:   procedure TForm1.btn1Click(Sender: TObj ...

  2. HDU4784 Dinner Coming Soon(dp)

    当时区域赛的一道题.题意大概是这样的,有一个1~N的图,然后你要从1->N,其中每经过一条边需要消耗你的时间和金钱,每到一个地方可以选择什么都不做,或者买一包盐,卖一包盐,身上不能同时有超过B包 ...

  3. jenkins忘记管理员账号密码的补救方法-转

    源引自:http://www.cnblogs.com/xiami303/p/3625829.html 一不小心,忘记了admin用户的账号密码.然后就看不到manage jenkins的那部分内容了, ...

  4. sshpass

    示例: ./sshpass -p ‘123456’  ssh -o StrictHostKeyChecking=no    root@192.168.1.15 ./sshpass -p ‘123456 ...

  5. cojs QAQ的图论题 题解报告

    话说这个题目应该叫做 斯特林数的逆袭 QAQ 先说一说部分分的算法 1.n<=5 直接暴力搜索就可以了 2.k=0的时候不难发现任意一张图的价值都是n,问题转化为计算有多少种图,显然是2^C(n ...

  6. Linux Shell查看磁盘分区,内存使用,CPU使用率

    Linux Shell查看磁盘分区,内存使用,CPU使用率 #!/bin/bash #disk_used_rate Location=/dev/xvdb Disk_Used_Rate=$(df -h ...

  7. Sqoop详细介绍包括:sqoop命令,原理,流程

    一简介 Sqoop是一个用来将Hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如 : MySQL ,Oracle ,Postgres等)中的数据导进到Hadoop的HDFS ...

  8. TOMCAT服务器不写端口号、不写项目名访问项目、虚拟目录配置

    一.不写端口. 这个问题都被问烂了,因为TOMCAT默认的访问端口为8080,而TCP/IP协议默认80端口访问,大家之所以看到别的网站都不写端口号是因为人家用的的80端口访问的,而80端口因为的TC ...

  9. Algorithm: cartesian tree

    http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_ ...

  10. framwork NHibernate

    NHibernate 一.NHibernate 1.HQL  curd语句总结 . 查询整个映射对象所有字段 ? //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段 String ...