Multi-thread exercises for beginners 2-snow effect, multithreading for beginners 2-snow
This exercise does not directly use the interface that inherits the Thread class or Runnable to implement multithreading, but uses an anonymous internal class.
Class to be imported:
1 import javax.swing.*;2 import java.awt.*;
1. Define the SowPanel class and inherit from the JPanel class. This class has two integer array members to save the start position of the snowflake. Assign an initial value to the array in the constructor, override the painting () method of the parent class, and define a startSnow () method that starts multithreading.
1 class SnowPanel extends JPanel 2 {3 // defines an integer array and stores the snowflake coordinate 4 private int [] x = new int [300]; 5 private int [] y = new int [300]; 6 7 public SnowPanel () 8 {9 // set the background to Black 10 setBackground (Color. black); 11 // use a random number to initialize the snowflake coordinate 12 for (int I = 0; I <x. length; I ++) 13 {14 x [I] = (int) (Math. random () * 800); 15 y [I] = (int) (Math. random () * 600); 16} 17} 18 19 public void paint (Graphics g) 20 {21 // Method 22 super that inherits the parent class painting. paint (g); 23 // set the color to White 24g. setColor (Color. white); 25 // use a loop to draw multiple snowflakes 26 for (int I = 0; I <x. length; I ++) 27 {28g. drawString ("*", x [I], y [I]); 29} 30} 31 // define a method to start multithreading, use the anonymous internal class 32 public void startSnow () 33 {34 new Thread () 35 {36 public void run () 37 {38 while (true) 39 {40 for (int I = 0; I <y. length; I ++) 41 {42 // coordinates move down 43 y [I] ++; 44 // check whether cross-border 45 if (y [I] = 600) 46 y [I] = 0; 47 // repaint 48 repaint (); 49} 50 51 try52 {53 Thread. sleep (10); 54} 55 catch (InterruptedException e) 56 {57 e. printStackTrace (); 58} 59} 60} 61 }. start (); 62} 63}
2. Define the ShowFrame class and inherit the Jframe class. In the constructor, set the Display Properties of the window, create a ShowPanel object, and add it to the window.
1 class SnowPanel extends JPanel 2 {3 // defines an integer array and stores the snowflake coordinate 4 private int [] x = new int [300]; 5 private int [] y = new int [300]; 6 7 public SnowPanel () 8 {9 // set the background to Black 10 setBackground (Color. black); 11 // use a random number to initialize the snowflake coordinate 12 for (int I = 0; I <x. length; I ++) 13 {14 x [I] = (int) (Math. random () * 800); 15 y [I] = (int) (Math. random () * 600); 16} 17} 18 19 public void paint (Graphics g) 20 {21 // Method 22 super that inherits the parent class painting. paint (g); 23 // set the color to White 24g. setColor (Color. white); 25 // use a loop to draw multiple snowflakes 26 for (int I = 0; I <x. length; I ++) 27 {28g. drawString ("*", x [I], y [I]); 29} 30} 31 // define a method to start multithreading, use the anonymous internal class 32 public void startSnow () 33 {34 new Thread () 35 {36 public void run () 37 {38 while (true) 39 {40 for (int I = 0; I <y. length; I ++) 41 {42 // coordinates move down 43 y [I] ++; 44 // check whether cross-border 45 if (y [I] = 600) 46 y [I] = 0; 47 // repaint 48 repaint (); 49} 50 51 try52 {53 Thread. sleep (10); 54} 55 catch (InterruptedException e) 56 {57 e. printStackTrace (); 58} 59} 60} 61 }. start (); 62} 63}
3. Run the slice (this is a static graph ):
Doubt: if others do not change, the effects of inheriting Frame, Panel, and inheriting JFrame and Jpanel are different. The former shows that the snow is always flickering; the latter is not blinking, and I personally feel that the latter is better.