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 ...
随机推荐
- spring_150805_datasource
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- js截取指定字节长度的字符串
默认的截取字符串都是根据字符长度或位置截取的,典型的两个方法是substr和substring. 这样导致的问题是截取同样长度的字符串时,多字节字符(汉字等)和单字节字符(半角英文字母.半角数字)占的 ...
- http://www.cnblogs.com/flyoung2008/archive/2013/08/11/3251148.html
http://www.cnblogs.com/flyoung2008/archive/2013/08/11/3251148.html
- hdu2024C语言合法标识符
#include<iostream> #include<stdio.h> #include<math.h> #include<stdlib.h> #in ...
- cojs QAQ的序列 解题报告
QAQ 这是从论文上搬的一道题目 但是由于并没有找到题目地址,所以就自己造数据咯 发现数据无比难造 (本题数据极弱,暴力或可AC?) 我们考虑离线的话其实只需要莫队就可以了 那么在线怎么做呢 二进制分 ...
- UITableViewCell的重用机制原理
UITableViewCell的重用机制原理 来自http://blog.csdn.net/omegayy/article/details/7356823 ====================== ...
- lintcode:数字组合I
数字组合I 给出一组候选数字(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T.C中的数字可以无限制重复被选取. 例如,给出候选数组[2,3,6,7]和目标数字7,所求的解为: [7], ...
- lintcode:带环链表
带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...
- iOS 深入Objective-C的动态特性
深入Objective-C的动态特性 Objective-C具有相当多的动态特性,基本的,也是经常被提到和用到的有动态类型(Dynamic typing),动态绑定(Dynamic binding)和 ...
- Android百度地图开发02之添加覆盖物 + 地理编码和反地理编码
下面来看一下地图上覆盖物的添加,以及地理编码和反地理编码. 添加覆盖物 在地图上添加覆盖物,一般需要以下几个步骤: 1. 定义坐标点,有可能是一个,有可能是多个(比如:多边形覆盖物). 2. 构造Ov ...