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. POJ 1222

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6196   Accepted: 40 ...

  2. redis系列之Redis应用场景

    1 取最新N个数据的操作 比如典型的取你网站的最新文章,通过下面方式,我们可以将最新的5000条评论的ID放在Redis的List集合中,并将超出集合部分从数据库获取 1)使用LPUSH latest ...

  3. Protobuf动态解析那些事儿

    需求背景 在接收到 protobuf 数据之后,如何自动创建具体的 Protobuf Message 对象,再做反序列化.“自动”的意思主要有两个方面:(1)当程序中新增一个 protobuf Mes ...

  4. hive-学习笔记

    1.hive模糊搜索表  show tables like '*name*'; 2.查看表结构信息  desc formatted table_name;  desc table_name; 3.查看 ...

  5. Java-J2SE学习笔记-字符串转化为二维数组

    1.字符串转化为二维Double数组 2.代码: package Test; public class TestDouble { public static void main(String[] ar ...

  6. linux驱动模型<输入子系统>

    在linux中提供一种输入子系统的驱动模型,其主要是实现在input.c中. 在输入子系统这套模型中,他把驱动分层分类.首先分为上下两层,上层为input.c .下层为驱动的实现,下层分为两部分,一部 ...

  7. MyBatis学习总结_03_优化MyBatis配置文件中的配置

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: 1 <?xml version=" ...

  8. Android videoview循环播放视频

    如何在一个activity里实现videoview对一个文件夹下的所有视频文件顺序循环播放,当切换界面时暂停, 切换回来继续原来的视频播放呢.所谓的 自动循环播放就是监听到视频播放结束之后继续重新播放 ...

  9. javascript whenReady

    var whenReady=(function(){ var funcs=[]; var ready=false; function handler(e){ if (ready) { return; ...

  10. jquerymobile UI使用

    <!DOCTYPE HTML> <html lang="zh-CN"> <head> <meta charset="utf-8& ...