• 初学多线程练习2--下雪效果


    此练习没有直接采用继承Thread类或者继承Runnable的接口来实现多线程,而是使用了匿名内部类。

    要导入的类:

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

    1.定义SowPanel类,继承JPanel类,这个类有两个整型数组成员,用来保存雪花起始位置。在构造函数中为数组赋初值;重写父类的paint()方法;定义一个启动多线程的startSnow()方法。

     1 class SnowPanel extends JPanel
     2 {
     3     //定义整型数组,存储雪花坐标
     4     private int[] x=new int[300];
     5     private int[] y=new int[300];
     6 
     7     public SnowPanel()
     8     {
     9         //设置背景为黑色
    10         setBackground(Color.black);
    11         //用随机数初始化雪花坐标
    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         //继承父类画的方法
    22         super.paint(g);
    23         //设置颜色为白色
    24         g.setColor(Color.white);
    25         //利用循环画出多个雪花
    26         for(int i=0;i<x.length;i++)
    27         {
    28             g.drawString("*",x[i],y[i]);
    29         }
    30     }
    31     //定义一个方法启动多线程,并使用匿名内部类
    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                         //坐标下移
    43                         y[i]++;
    44                         //检查是否越界
    45                         if(y[i]==600)
    46                         y[i]=0;
    47                         //重绘
    48                         repaint();
    49                     }
    50                     
    51                      try
    52                      {
    53                         Thread.sleep(10);
    54                      }
    55                          catch (InterruptedException e)
    56                      {
    57                         e.printStackTrace();
    58                      }
    59                 }
    60             }
    61         }.start();
    62     }
    63 }

    2.定义ShowFrame类,继承Jframe 类。在构造方法中设置窗口的显示属性,并创建ShowPanel对象,添加到窗口中。

    
    
     1 public class SnowFrame extends JFrame
     2 {
     3     public SnowFrame()
     4     {
     5         setTitle("满天雪花");
     6         //设置窗体显示坐标和大小
     7         setBounds(100,100,800,600);
     8         //设置用户在此窗体上发起close时默认的操作
     9         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    10         //设置此窗体不可被调整大小
    11         setResizable(false);
    12         SnowPanel p=new SnowPanel();
    13         //调用能够启动线程的方法
    14         p.startSnow();
    15         //把SnowPanel对象添加到此窗体上
    16         this.add(p);
    17         this.setVisible(true);
    18     }
    19     public static void main(String[] args)
    20     {new SnowFrame();}
    21 }
    
    
    

    3.运行效果图片(此为静态图):

    疑问:发现其他的不改变,继承Frame、Panel和继承JFrame、Jpanel的运行效果是不同的,前者显示的下雪场景总是有闪烁的感觉;后者则没有闪烁,个人感觉后者效果更好些。

     

  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    How to check if one path is a child of another path?
    Why there is two completely different version of Reverse for List and IEnumerable?
    在Jenkins中集成Sonarqube
    如何查看sonarqube的版本 how to check the version of sonarqube
    Queue
    BFS广度优先 vs DFS深度优先 for Binary Tree
    Depth-first search and Breadth-first search 深度优先搜索和广度优先搜索
    102. Binary Tree Level Order Traversal 广度优先遍历
    How do I check if a type is a subtype OR the type of an object?
  • 原文地址:https://www.cnblogs.com/wsw-tcsygrwfqd/p/4985701.html
Copyright © 2020-2023  润新知