码迷,mamicode.com
首页 > 编程语言 > 详细

Java MovingBall (整理)

时间:2016-01-02 16:25:09      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

  1 package demo;
  2 
  3 /**
  4  *                           Java MovingBall (整理)
  5  * 声明:
  6  *     这份源代码没有注释,已经忘记了为什么要写他了,基本上应该是因为当时觉得好玩吧。
  7  * 有时候想想,也许是因为当时就是想做一个这样的效果的东西。
  8  *
  9  *                                   2016-1-2 深圳 南山平山村 曾剑锋
 10  */
 11 
 12 import java.awt.Color;
 13 import java.awt.Graphics;
 14 
 15 import javax.swing.JFrame;
 16 import javax.swing.JPanel;
 17 
 18 public class MovingBall extends JPanel {
 19 
 20     private static final long serialVersionUID = 1L;
 21     static int centerX = 600/2;
 22     static int centerY = 600/2;
 23     final Ball ball = new Ball(centerX, centerY);
 24     boolean cicleflag = true;
 25     int cicleSemi = 0;
 26     double angle = 0;
 27     int ballCicle = 30;
 28     public MovingBall() {
 29         start();
 30     }
 31     @Override
 32     public void paint(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 void start() {
 44         new Thread(new Runnable() {
 45             
 46             @Override
 47             public void run() {
 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 void ballMove(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 void main(String[] args) {
 86         JFrame jFrame = new JFrame();
 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 = new MovingBall();
 93         jFrame.add(moveBall);
 94         jFrame.setVisible(true);
 95     }
 96 }
 97 
 98 class Ball{
 99     static int step = 10;
100     int x;
101     int y;
102     int dx;
103     int dy;
104     double angle;
105     int cicle = 30;
106     public Ball(int x, int y) {
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, int angle) {
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 void drawing(Graphics graphics) {
131         graphics.setColor(Color.white);
132         graphics.fillArc(this.x-cicle/2, this.y-cicle/2, cicle, cicle, 0, 360);
133     }
134 }

效果如图:

技术分享

技术分享

技术分享

Java MovingBall (整理)

标签:

原文地址:http://www.cnblogs.com/zengjfgit/p/5094848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!