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