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