Java multithreaded programming to achieve snow effect _java

Source: Internet
Author: User
Tags anonymous set background

Instead of implementing multithreading directly by inheriting thread classes or inheriting runnable interfaces, anonymous inner classes are used.

Classes to import:

 Import javax.swing.*;
 Import java.awt.*;

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

Class Snowpanel extends JPanel {//define integer array, store snowflake coordinates private int[] x=new int[300];

  Private int[] Y=new int[300];
    Public Snowpanel () {//Set Background to Black SetBackground (Color.Black);
      Initializes the snowflake coordinate for (int i=0;i<x.length;i++) {x[i]= (int) (Math.random () *800) with random numbers;
    y[i]= (int) (Math.random () *600);
    } public void Paint (Graphics g) {//Inheritance Parent painting Method Super.paint (g);
    Set the color to white g.setcolor (color.white);
    Use loops to draw multiple snowflakes for (int i=0;i<x.length;i++) {g.drawstring ("*", x[i],y[i]);
        }//Define a method to start multiple threads and use anonymous inner class public void Startsnow () {new Thread () {public void run () {
            while (true) {for (int i=0;i<y.length;i++) {//coordinate move down y[i]++;
            Check for Cross-border if (y[i]==600) y[i]=0;
          Redraw repaint ();
           try {thread.sleep (10); } CatCH (interruptedexception e) {e.printstacktrace ();
  }}}.start ();

 }
}

2. Define the Showframe class and inherit the JFrame class. Set the display properties of the window in the constructor and create the Showpanel object to add to the window.

 Class Snowpanel extends JPanel {//define integer array, store snowflake coordinates private int[] x=new int[300];
 
   Private int[] Y=new int[300];
     Public Snowpanel () {//Set Background to Black SetBackground (Color.Black);
       Initializes the snowflake coordinate for (int i=0;i<x.length;i++) {x[i]= (int) (Math.random () *800) with random numbers;
     y[i]= (int) (Math.random () *600);
     } public void Paint (Graphics g) {//Inheritance Parent painting Method Super.paint (g);
     Set the color to white g.setcolor (color.white);
     Use loops to draw multiple snowflakes for (int i=0;i<x.length;i++) {g.drawstring ("*", x[i],y[i]);
       }//Define a method to start multiple threads and use anonymous inner class public void Startsnow () {new Thread () {public void run ()
             {while (true) {for (int i=0;i<y.length;i++) {//coordinate Move Down
             y[i]++;
             Check for Cross-border if (y[i]==600) y[i]=0;
           Redraw repaint ();
         } try {    Thread.Sleep (10);
           catch (Interruptedexception e) {e.printstacktrace ();
   }}}.start ();

 }
 }

3. Run effect picture (this is a static diagram):

Question: Discover other unchanged, inherit frame, panel and inherit JFrame, JPanel the operation effect is different, the former shows the snow scene always has a flicker of feeling, the latter is not flashing, the person feels the latter effect is better.

Now let's take a look at an example

Here we make a complete example of the difference between the threads produced by the open-wire process and the differences in the resulting thread:

Package debug;

Import java.io.*;
Import Java.lang.Thread;


Class Mythread extends thread{public
 int x = 0;

 public void Run () {
  System.out.println (++x);
 }
}

Class R implements runnable{
 private int x = 0;
 public void Run () {
  System.out.println (++x);
 }
}

public class Test {public
 static void Main (string[] args) throws exception{for
  
  (int i=0;i<10;i++) {
   Thread t = new mythread ();
   T.start ();
  }
  Thread.Sleep (10000);//Let the above thread run complete
  r r = new R ();
  for (int i=0;i<10;i++) {
   thread t = new Thread (r);
   T.start ();}}


The 10 threads generated by the 10 thread objects above are printed 10 times 1. The 10 threads produced by the following 10 thread objects print 1 to 10 when they run. We refer to the following 10 threads as multiple threads of the same instance (runnable instance).

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.