Beginner Multithreading Practice 2--snow effect

Source: Internet
Author: User
Tags set background

This exercise does not implement multi-threading directly with an interface that inherits the thread class or inherits runnable, but rather uses an anonymous inner class.

Classes to import:

1 import javax.swing.*; 2 import java.awt.*;

1. Define the Sowpanel class, inheriting the JPanel class, which has two integer array members to hold the snowflake start position. Assigns an initial value to the array in the constructor, overrides the paint () method of the parent class, and defines a Startsnow () method that initiates multithreading.

1 classSnowpanelextendsJPanel2 {3     //defining an integer array, storing snowflake coordinates4     Private int[] x=New int[300];5     Private int[] y=New int[300];6 7      PublicSnowpanel ()8     {9         //set Background to blackTen SetBackground (color.black); One         //Initialize snowflake coordinates with random numbers A          for(inti=0;i<x.length;i++) -         { -X[i]= (int) (Math.random () *800); theY[i]= (int) (Math.random () *600); -         } -     } -      +      Public voidPaint (Graphics g) -     { +         //how to inherit a parent class drawing A         Super. Paint (g); at         //set color to white - G.setcolor (color.white); -         //draw multiple snowflakes with loops -          for(inti=0;i<x.length;i++) -         { -g.DrawString ("*", X[i],y[i]); in         } -     } to     //define a method to start multi-threading and use anonymous inner classes +      Public voidStartsnow () -     { the         NewThread () *         { $              Public voidRun ()Panax Notoginseng             { -                  while(true) the                 { +                      for(inti=0;i<y.length;i++) A                     { the                         //coordinate Move Down +y[i]++; -                         //Check to see if it crosses $                         if(y[i]==600) $Y[i]=0; -                         //Redraw - repaint (); the                     } -                     Wuyi                      Try the                      { -Thread.Sleep (10); Wu                      } -                          Catch(interruptedexception e) About                      { $ e.printstacktrace (); -                      } -                 } -             } A }.start (); +     } the}

2. Define the Showframe class and inherit the JFrame class. In the construction method, set the window's display properties and create the Showpanel object, which is added to the window.

1 classSnowpanelextendsJPanel2 {3     //defining an integer array, storing snowflake coordinates4     Private int[] x=New int[300];5     Private int[] y=New int[300];6 7      PublicSnowpanel ()8     {9         //set Background to blackTen SetBackground (color.black); One         //Initialize snowflake coordinates with random numbers A          for(inti=0;i<x.length;i++) -         { -X[i]= (int) (Math.random () *800); theY[i]= (int) (Math.random () *600); -         } -     } -      +      Public voidPaint (Graphics g) -     { +         //how to inherit a parent class drawing A         Super. Paint (g); at         //set color to white - G.setcolor (color.white); -         //draw multiple snowflakes with loops -          for(inti=0;i<x.length;i++) -         { -g.DrawString ("*", X[i],y[i]); in         } -     } to     //define a method to start multi-threading and use anonymous inner classes +      Public voidStartsnow () -     { the         NewThread () *         { $              Public voidRun ()Panax Notoginseng             { -                  while(true) the                 { +                      for(inti=0;i<y.length;i++) A                     { the                         //coordinate Move Down +y[i]++; -                         //Check to see if it crosses $                         if(y[i]==600) $Y[i]=0; -                         //Redraw - repaint (); the                     } -                     Wuyi                      Try the                      { -Thread.Sleep (10); Wu                      } -                          Catch(interruptedexception e) About                      { $ e.printstacktrace (); -                      } -                 } -             } A }.start (); +     } the}

3. Run the slice (this is a static diagram):

Doubt: Found other unchanged, inherit frame, panel and inherit JFrame, JPanel operation effect is different, the former shows the snow scene always has a flashing feeling, the latter is not flashing, the latter is more effective.

Beginner Multithreading Practice 2--snow effect

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.