On-machine topics (beginner)-Snowball (Java)

Source: Internet
Author: User

The last section realizes the effect of the stars, this section will implement a small instance of Snowball, here to use the redrawing and threading knowledge, the code is as follows:

Import Java.awt.color;import java.awt.frame;import Java.awt.graphics;import Java.awt.panel;public class SnowBall { public static void Main (string[] args) {frame frame=new frame (); Frame.setbackground (Color.Black); Frame.setsize (1024, 768); Mypanel mypanel=new Mypanel (); Frame.add (Mypanel); Thread thread=new thread (mypanel); Thread.Start (); Frame.show ();}} Class Mypanel extends Panel implements Runnable{int X=120;int y=10; @Overridepublic void Paint (Graphics g) {G.setcolor (Col Or. white); G.filloval (x, Y, 20, 20);} @Overridepublic void Run () {while (true) {y++;if (y>768) {y=0;} Repaint ();}}}

At this point the run found the small ball does not move, but it is not the problem of the code, because the CPU is running too fast, here we can let the thread sleep for a while to execute the next time, in the while add:

try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}

Run the instance again to see that the ball is constantly falling.


On-machine topics (beginner)-Snowball (Java)

Related Article

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.