1 packagedemo;2
3 /**
4 * Java 碰撞的球 MovingBall (整理)5 * 声明:6 * 这份源代码没有注释,已经忘记了为什么要写他了,基本上应该是因为当时觉得好玩吧。7 * 有时候想想,也许是因为当时就是想做一个这样的效果的东西。8 *9 * 2016-1-2 深圳 南山平山村 曾剑锋10 */
11
12 importjava.awt.Color;13 importjava.awt.Graphics;14
15 importjavax.swing.JFrame;16 importjavax.swing.JPanel;17
18 public class MovingBall extendsJPanel {19
20 private static final long serialVersionUID = 1L;21 static int centerX = 600/2;22 static int centerY = 600/2;23 final Ball ball = newBall(centerX, centerY);24 boolean cicleflag = true;25 int cicleSemi = 0;26 double angle = 0;27 int ballCicle = 30;28 publicMovingBall() {29 start();30 }31 @Override32 public voidpaint(Graphics graphics) {33 super.paint(graphics);34 this.setBackground(Color.black);35 graphics.setColor(Color.BLUE);36 graphics.fillArc(centerX-cicleSemi/2, centerY-cicleSemi/2, cicleSemi, cicleSemi, 0, 360);37 ball.drawing(graphics);38 graphics.setColor(Color.white);39 graphics.drawArc(centerX-cicleSemi/2, centerY-cicleSemi/2, cicleSemi, cicleSemi, 0, 360);40 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);41 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);42 }43 public voidstart() {44 new Thread(newRunnable() {45
46 @Override47 public voidrun() {48 while (true) {49 try{50 ballMove(ball);51
52 if (cicleflag == true) {53 cicleSemi += 2;54 if (cicleSemi >centerX) {55 cicleflag = false;56 }57 }else{58 cicleSemi -= 2;59 if (cicleSemi < 0) {60 cicleflag = true;61 }62 }63 angle ++;64 repaint();65 Thread.sleep(20);66 } catch(InterruptedException e) {67 e.printStackTrace();68 }69 }70
71 }72
73 public voidballMove(Ball ball) {74 ball.x +=ball.dx;75 ball.y +=ball.dy;76 if (ball.x < 50 || ball.x > centerX*2-50) {77 ball.dx = -ball.dx;78 }else if (ball.y < 50 || ball.y > centerY*2-50) {79 ball.dy = -ball.dy;80 }81 }82 }).start();83 }84
85 public static voidmain(String[] args) {86 JFrame jFrame = newJFrame();87 jFrame.setTitle("MovingBall");88 jFrame.setSize(centerX*2, centerY*2);89 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);90 jFrame.setLocationRelativeTo(null);91
92 MovingBall moveBall = newMovingBall();93 jFrame.add(moveBall);94 jFrame.setVisible(true);95 }96 }97
98 classBall{99 static int step = 10;100 intx;101 inty;102 intdx;103 intdy;104 doubleangle;105 int cicle = 30;106 public Ball(int x, inty) {107 this.x =x;108 this.y =y;109 this.angle = Math.random()*360;110 if (angle <= 180) {111 dx = (int) (step*Math.cos(angle*Math.PI/180));112 dy = (int) (step*Math.sin(angle*Math.PI/180));113 }else{114 dx = (int) (step*Math.cos(angle*Math.PI/180));115 dy = -(int) (step*Math.sin(angle*Math.PI/180));116 }117 }118 public Ball(int x, int y, intangle) {119 this.x =x;120 this.y =y;121 this.angle =angle;122 if (angle <= 180) {123 dx = (int) (step*Math.cos(angle*Math.PI/180));124 dy = (int) (step*Math.sin(angle*Math.PI/180));125 }else{126 dx = (int) (step*Math.cos(angle*Math.PI/180));127 dy = -(int) (step*Math.sin(angle*Math.PI/180));128 };129 }130 public voiddrawing(Graphics graphics) {131 graphics.setColor(Color.white);132 graphics.fillArc(this.x-cicle/2, this.y-cicle/2, cicle, cicle, 0, 360);133 }134 }