Java Movingball (Collation)

Source: Internet
Author: User

1  Packagedemo;2 3 /**4 * Java Movingball (collation)5 * Disclaimer:6 * This source code does not have the comment, has forgotten why to write him, basically should be because at that time thought the fun. 7 * Sometimes think about it, maybe because that's what you want to do with that effect. 8  *9 * 2016-1-2 Shenzhen Nanshan Ping Shan village ZengjianfengTen  */ One  A ImportJava.awt.Color; - ImportJava.awt.Graphics; -  the ImportJavax.swing.JFrame; - ImportJavax.swing.JPanel; -  -  Public classMovingballextendsJPanel { +  -     Private Static Final LongSerialversionuid = 1L; +     Static intCenterX = 600/2; A     Static intCenterY = 600/2; at     FinalBall Ball =NewBall (CenterX, centery); -     BooleanCicleflag =true; -     intCiclesemi = 0; -     DoubleAngle = 0; -     intBallcicle = 30; -      PublicMovingball () { in start (); -     } to @Override +      Public voidPaint (Graphics graphics) { -         Super. Paint (graphics); the          This. SetBackground (color.black); * Graphics.setcolor (color.blue); $Graphics.fillarc (CENTERX-CICLESEMI/2, CENTERY-CICLESEMI/2, Ciclesemi, Ciclesemi, 0, 360);Panax Notoginseng ball.drawing (graphics); - Graphics.setcolor (color.white); theGraphics.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); AGraphics.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); the     } +      Public voidstart () { -         NewThread (NewRunnable () { $              $ @Override -              Public voidrun () { -                  while(true) { the                     Try { - ballmove (ball);Wuyi                          the                         if(Cicleflag = =true) { -Ciclesemi + = 2; Wu                             if(Ciclesemi >CenterX) { -Cicleflag =false; About                             } $}Else { -Ciclesemi-= 2; -                             if(Ciclesemi < 0) { -Cicleflag =true; A                             } +                         } theAngle + +; - repaint (); $Thread.Sleep (20); the}Catch(interruptedexception e) { the e.printstacktrace (); the                     } the                 } -                  in             } the  the              Public voidballmove (Ball ball) { AboutBall.x + =Ball.dx; theBall.y + =Ball.dy; the                 if(Ball.x < | | ball.x > CENTERX*2-50) { theBALL.DX =-Ball.dx; +}Else if(Ball.y < | | ball.y > CENTERY*2-50) { -Ball.dy =-Ball.dy; the                 }Bayi             } the }). Start (); the     }  -      -      Public Static voidMain (string[] args) { theJFrame JFrame =NewJFrame (); theJframe.settitle ("Movingball"); theJframe.setsize (centerx*2, centery*2); the jframe.setdefaultcloseoperation (jframe.exit_on_close); -Jframe.setlocationrelativeto (NULL); the          theMovingball Moveball =NewMovingball (); the Jframe.add (moveball);94Jframe.setvisible (true); the     } the } the 98 classball{ About     Static intStep = 10; -     intx;101     inty;102     intDX;103     intdy;104     Doubleangle; the     intCicle = 30;106      PublicBall (intXinty) {107          This. x =x;108          This. y =y;109          This. Angle = Math.random () *360; the         if(Angle <= 180) {111DX = (int) (Step*math.cos (angle*math.pi/180)); theDY = (int) (Step*math.sin (angle*math.pi/180));113}Else { theDX = (int) (Step*math.cos (angle*math.pi/180)); theDY =-(int) (Step*math.sin (angle*math.pi/180)); the         }117     }118      PublicBall (intXintYintangle) {119          This. x =x; -          This. y =y;121          This. Angle =angle;122         if(Angle <= 180) {123DX = (int) (Step*math.cos (angle*math.pi/180));124DY = (int) (Step*math.sin (angle*math.pi/180)); the}Else {126DX = (int) (Step*math.cos (angle*math.pi/180));127DY =-(int) (Step*math.sin (angle*math.pi/180)); -         };129     } the      Public voidDrawing (Graphics graphics) {131 Graphics.setcolor (color.white); theGraphics.fillarc ( This. X-CICLE/2, This. Y-CICLE/2, Cicle, cicle, 0, 360);133     }134}

Effect

Java Movingball (grooming)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.