Java 碰撞的球 MovingBall (整理)
2016-01-02 16:00
239 查看
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多线程并发)控制并发线程数的Semaphore
- Java RGB数组图像合成 ImageCombining (整理)
- 隐马尔可夫模型中基于比例因子的前向算法(java实现)
- 使用Spring的注解方式实现AOP
- maven仓库配置
- Java 动态眨眼 EyesJPanel (整理)
- 【Java】斐波那契数列前N项(堆栈算法)
- eclipse配置步骤
- Thinking in Java---异常处理机制
- 仲恺ACM 1010:平均绩点【java】
- Java 动态太极图 DynamicTaiChi (整理)
- Java中的简单工厂模式
- Java 炫舞按键功能 DancingPlay (整理)
- Eclipse/MyEclipse字体走样
- Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean
- Java 图片转换为字符图 CharMaps (整理)
- 16.怎样自学Struts2之Struts2异常处理[视频]
- 【jvm】java垃圾回收
- java中String类比较常用的方法
- Java Cardioid 心脏形曲线 (整理)