• 雪花飘啊飘


     1 package snow2;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.IOException;
     5 
     6 public class Test {
     7     public static void main(String[] args) throws FileNotFoundException, IOException {
     8         MyFrame frame = new MyFrame();
     9         frame.init();
    10         
    11         
    12         
    13     }
    14
    15}






     1 package snow2;
     2 
     3 import java.awt.image.BufferedImage;
     4 import java.io.File;
     5 import java.io.FileInputStream;
     6 import java.io.FileNotFoundException;
     7 import java.io.IOException;
     8 
     9 import javax.imageio.ImageIO;
    10 import javax.swing.ImageIcon;
    11 import javax.swing.JFrame;
    12 import javax.swing.JLabel;
    13 
    14 public class MyFrame {
    15     
    16     private JFrame frame ; 
    17     private MyPanel panel;
    18     private JLabel label ;
    19     
    20     public void addPanel(){
    21         panel = new MyPanel();
    22         frame.add(panel);
    23         panel.Down();
    24         
    25     }
    26     
    27     /**]
    28      *构建窗体
    29      * @throws IOException 
    30      * @throws FileNotFoundException 
    31      */
    32     public void init() throws FileNotFoundException, IOException{
    33         String path = "t8.jpg";
    34         File file = new File(path);
    35         //创建图片流
    36         BufferedImage bi =ImageIO .read(new FileInputStream(file));
    37         frame = new JFrame();
    38         label = new JLabel(new ImageIcon(bi));
    39         
    40         addPanel();
    41         //添加图片在面板中
    42         panel.add(label);
    43         //设置此窗体是否可以改变大小
    44         frame.setResizable(false);
    45         //设置title
    46         frame.setTitle("下雪了");
    47         //设置尺寸
    48         frame.setSize(800, 600);
    49         //设置窗体在最中间
    50         frame.setLocationRelativeTo(null);
    51         //设置程序始终在最前端
    52         frame.setAlwaysOnTop(true);
    53         //设置关闭窗体时结束程序
    54         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    55         //设置窗体始终是可见的,一定要放在程序的最下方
    56         
    57         frame.setVisible(true);
    58 
    59 
    60     }
    61 }
    
    
    
     
      1 package snow;
      2 
      3 import java.awt.Color;
      4 import java.awt.Font;
      5 import java.awt.Graphics;
      6 
      7 import javax.swing.JPanel;
      8 
      9 /**
     10  *面板中对雪花进行操作 
     11  */
     12 
     13 public class MyPanel extends JPanel{
     14 
     15     
     16     
     17     //横向 和 竖向 存储雪花的集合
     18     private int[] xx ;
     19     private int[] yy ;
     20     
     21     //存放不同雪花字体的数组
     22     private Font[] fs ;
     23     
     24     
     25     /**
     26      *空参构造 
     27      */
     28     public MyPanel(){
     29         
     30         
     31         //横向 和 竖向 存储雪花的数目
     32         xx = new int[300];
     33         yy = new int[300];
     34         
     35         
     36         //雪花在窗体中产生的位置
     37         for(int i = 0 ; i < xx.length ; i ++){
     38             xx[i] = (int)(Math.random() * 800);
     39             yy[i] = (int)(Math.random() * 600);
     40         }
     41         
     42         fs = new Font[15];
     43         //
     44         for(int i = 0 ; i < fs.length ; i ++){
     45             //Font("是什么字体" , "设置字体是粗体...","设置字体的大小");
     46             fs[i] = new Font("宋体", Font.BOLD, 12 + i );
     47         }
     48     }
     49 
     50     /**
     51      *paint() 重写   切传入一个画笔对象 
     52      */
     53     //系统自动执行
     54     @Override
     55     public void paint(Graphics g) {
     56         super.paint(g);
     57         
     58         //改变当前的背景颜色
     59         this.setBackground(Color.BLACK);
     60         //设置画笔的颜色
     61         g.setColor(Color.WHITE);
     62         
     63         //
     64         for(int i = 0 ; i < xx.length ; i ++){
     65             //设置每个位置的字体
     66             g.setFont(fs[i%15]);
     67             //画笔对象写入的位置
     68             g.drawString("*", xx[i], yy[i]);
     69         }
     70     }
     71     
     72     /**
     73      *雪花飘落 
     74      */
     75     public void down(){
     76         //线程
     77         new Thread(){
     78             public void run(){
     79                 //不确定停止的时间,
     80                 while(true){
     81                     /**
     82                      *往下飘
     83                      *x坐标不变, y坐标增加 
     84                      */
     85                     for(int i = 0 ; i < yy.length ; i ++){
     86                         yy[i]+=3;
     87                         
     88                         //每一个雪花下落到最下方的时候,再次从新开始出现
     89                         if(yy[i] > 600){
     90                             yy[i] = 0;
     91                         }
     92                     }
     93                     
     94                     for(int i = 0 ; i < xx.length ; i ++){
     95                         xx[i]++;
     96                         
     97                         if(xx[i]>=800){
     98                             xx[i]=0;
     99                         }
    100                     }
    101                     
    102                     //刷屏
    103                     repaint();
    104                     
    105                     //睡眠时间
    106                     try {
    107                         Thread.sleep(20);
    108                     } catch (InterruptedException e) {
    109                         e.printStackTrace();
    110                     }
    111                 }
    112             }
    113         }.start();
    114     }
    115     
    116     
    117     
    118 }

    效果实现

  • 相关阅读:
    python异常处理与上下文管理器
    SpringBoot-MongoDB 索引冲突分析及解决
    补习系列(13)-springboot redis 与发布订阅
    带着canvas去流浪系列之八 碰撞
    带着canvas去流浪系列之九 粒子动画
    通过哪吒动漫豆瓣影评,带你分析python爬虫与BeautifulSoup快速入门
    Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...
    免费试用 | 多模 NoSQL 服务GeminiDB for Cassandra 全球首发
    Vue+ElementUI项目使用webpack输出MPA
    shell的shift用法
  • 原文地址:https://www.cnblogs.com/sdfd101/p/4899490.html
Copyright © 2020-2023  润新知