Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 419|回复: 0

[Java基础知识]Dos窗口下雪程序

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-9-30 17:39:38 | 显示全部楼层 |阅读模式
    1. import java.applet.AudioClip;
    2. import java.awt.Dimension;
    3. import java.awt.Graphics;
    4. import java.awt.Image;
    5. import java.awt.Toolkit;
    6. import java.io.IOException;
    7. import java.net.URL;
    8. import java.util.Iterator;
    9. import java.util.Random;
    10. import java.util.Vector;
    11. import java.util.concurrent.locks.Lock;
    12. import java.util.concurrent.locks.ReentrantLock;
    13. import javax.imageio.ImageIO;
    14. import javax.swing.JApplet;
    15. import javax.swing.JPanel;
    16. import javax.swing.JWindow;
    17. import com.sun.awt.AWTUtilities;
    18. /**
    19. * @author lifetime
    20. *
    21. */
    22. public class MerryChristmas extends JWindow implements Runnable {
    23. private static final int[] WindType = new int[] { -1, 1 };
    24. private Image[] xueHuaImages;
    25. private Dimension screenSize;
    26. private Vector
    27.    
    28.       list;
    29. private Lock lock;
    30. public MerryChristmas() {
    31.   list = new Vector
    32.      
    33.       ();
    34.   lock = new ReentrantLock();
    35.   initImages();
    36.   setAlwaysOnTop(true);
    37.   JPanel rootPanel = new JPanel();
    38.   setContentPane(rootPanel);
    39.   screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    40.   setSize(screenSize);
    41.   AWTUtilities.setWindowOpaque(this, false);
    42.   URL audioPath = getClass().getResource("backgroundmusic.mid");
    43.   AudioClip audio = JApplet.newAudioClip(audioPath);
    44.   audio.loop();
    45.   this.setVisible(true);
    46.   Thread t = new Thread(new Runnable() {
    47.     public void run() {
    48.       while (true) {
    49.        try {
    50.         Thread.sleep(300);
    51.         create();
    52.         repaint();
    53.        } catch (InterruptedException e) {
    54.       }
    55.     }
    56.    }
    57.   });
    58.   t.start();
    59. }
    60. public void paint(Graphics g) {
    61.    super.paint(g);
    62.    if (lock.tryLock()) {
    63.     try {
    64.       for (Iterator
    65.       
    66.         iterator = list.iterator(); iterator.hasNext();) {
    67.       XueHua xh = iterator.next();
    68.       g.drawImage(xh.img, xh.x, xh.y, null);
    69.     }
    70.     } finally {
    71.      lock.unlock();
    72.     }
    73.    }
    74. }
    75.   void initImages() {
    76.    xueHuaImages = new Image[4];
    77.    try {
    78.      for (int i = 1; i <= 4; i++) {
    79.        xueHuaImages[i - 1] = ImageIO.read(getClass().getResourceAsStream(i + ".png"));
    80.      }
    81.    } catch (IOException e) {
    82.      e.printStackTrace();
    83.    }
    84.   }
    85.   protected void create() {
    86.     XueHua e = randomNext(0);
    87.     if (lock.tryLock()) {
    88.      try {
    89.       list.add(e);
    90.      } finally {
    91.       lock.unlock();
    92.      }
    93.     }
    94.   }
    95.   XueHua randomNext(int y) {
    96.    Random random = new Random();
    97.    int x = random.nextInt(screenSize.width);
    98.    XueHua hua = new XueHua();
    99.    hua.x = x;
    100.    hua.y = y;
    101.    hua.wind = WindType[random.nextInt(2)];
    102.    hua.windSpeed = hua.wind * 3;
    103.    hua.img = xueHuaImages[random.nextInt(xueHuaImages.length)];
    104.    hua.x += hua.img.getWidth(null);
    105.    if (hua.x >= screenSize.width) {
    106.      hua.x = screenSize.width - hua.img.getWidth(null);
    107.     }
    108.    return hua;
    109.   }
    110.   public void run() {
    111.    while (true) {
    112.     try {
    113.       Thread.sleep(50);
    114.       down();
    115.       repaint();
    116.     } catch (InterruptedException e) {
    117.     }
    118.    }
    119. }
    120. protected void down() {
    121.    if (lock.tryLock()) {
    122.      try {
    123.        for (Iterator
    124.       
    125.          it = list.iterator(); it.hasNext();) {
    126.         XueHua xh = it.next();
    127.         if (xh.y > screenSize.height) {
    128.          it.remove();
    129.          continue;
    130.         }
    131.         if (xh.x < 0 || xh.x > (screenSize.width - xh.img.getWidth(null))) {
    132.            xh.wind *= -1;
    133.             xh.windSpeed *= -1;
    134.           }
    135.         xh.x += xh.windSpeed;
    136.         xh.y += 3;
    137.      }
    138.      } finally {
    139.         lock.unlock();
    140.      }
    141.    }
    142. }
    143.   class XueHua {
    144.     public int x;
    145.     public int y;
    146.     public int wind;
    147.     public int windSpeed;
    148.     public Image img;
    149.   }
    150.   public static void main(String[] args) {
    151.     Thread start = new Thread(new MerryChristmas());
    152.       start.start();
    153.    }
    154. }
    155.       
    156.       
    157.      
    158.    
    复制代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-20 18:07 , Processed in 0.378696 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表