Using Java to achieve a continuously moving ball

Source: Internet
Author: User

Original works, can be reproduced, but please mark the source address http://www.cnblogs.com/V1haoge/p/5559829.html

Use the Java JFrame Framework to achieve a continuously moving ball for your own study only.

The core part is the move () method for moving the ball, which bounces when the ball touches the wall walls and does not move outside the wall.

1 /**2 * Use Java to achieve a continuously moving ball3  */4 5  PackageJFrame;6 7 ImportJava.awt.Color;8 ImportJava.awt.Graphics;9 Ten ImportJavax.swing.JFrame; One  A  Public classJframetestextendsjframe{ -     //Defining Move Variables -     intx = 0; the     inty = 0; -     intm = 1; -     intn = 1; -     //Main function +      Public Static voidMain (string[] args) { -         Newjframetest (); +     } A     //use the constructor to create a form and set at      Publicjframetest () { -          This. setvisible (true); -          This. SetSize (500,500); -          This. setdefaultcloseoperation (3); -          This. setlocation (400, 100); -          This. setresizable (false); in          This. Settitle ("Test Framework"); - move (); to     } +     //Overriding paint Methods - @Override the      Public voidPaint (Graphics g) { *         Super. Paint (g); $ G.setcolor (color.black);Panax NotoginsengG.filloval (x, + y, 50, 50); -     } the     //define the ball movement trajectory +      Public voidMove () { A          while(true){ theX + =m; +Y + =N; -             if(x >= 450){ $m =-m; $             } -             if(X < 0){ -m = 1; the             } -             if(Y >= 425){Wuyin =-N; the             } -             if(Y < 0){ Wun = 1; -             } About             Try { $Thread.Sleep (1); -}Catch(interruptedexception e) { - e.printstacktrace (); -             } A repaint (); +         } the     } -}

Using Java to achieve a continuously moving ball

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.