zoukankan      html  css  js  c++  java
  • java版的下雪,大家圣诞快乐

    1. [代码][Java]代码    
    package com.yk.tools.game;
     
    import java.applet.AudioClip;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Iterator;
    import java.util.Random;
    import java.util.Vector;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
     
    import javax.imageio.ImageIO;
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    import javax.swing.JWindow;
     
    import com.sun.awt.AWTUtilities;
     
    /**
     * @author lifetime
     * 
     */
    public class MerryChristmas extends JWindow implements Runnable {
        private static final int[] WindType = new int[] { -1, 1 };
        private Image[] xueHuaImages;
        private Dimension screenSize;
        private Vector<XueHua> list;
        private Lock lock;
     
        public MerryChristmas() {
            list = new Vector<XueHua>();
            lock = new ReentrantLock();
            initImages();
            setAlwaysOnTop(true);
            JPanel rootPanel = new JPanel();
            setContentPane(rootPanel);
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setSize(screenSize);
            AWTUtilities.setWindowOpaque(this, false);
            URL audioPath = getClass().getResource("music.wav");
            AudioClip audio = JApplet.newAudioClip(audioPath);
            audio.loop();
            this.setVisible(true);
            Thread t = new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(300);
                            create();
                            repaint();
                        } catch (InterruptedException e) {
                        }
                    }
                }
            });
            t.start();
        }
     
        public void paint(Graphics g) {
            super.paint(g);
            if (lock.tryLock()) {
                try {
                    for (Iterator<XueHua> iterator = list.iterator(); iterator.hasNext();) {
                        XueHua xh = iterator.next();
                        g.drawImage(xh.img, xh.x, xh.y, null);
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
     
        void initImages() {
            xueHuaImages = new Image[4];
            try {
                for (int i = 1; i <= 4; i++) {
                    xueHuaImages[i - 1] = ImageIO.read(getClass().getResourceAsStream(i + ".png"));
                }http://www.enterdesk.com/special/shouhui/手绘图片
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        protected void create() {
            XueHua e = randomNext(0);
            if (lock.tryLock()) {
                try {
                    list.add(e);
                } finally {
                    lock.unlock();
                }
            }
        }
     
        XueHua randomNext(int y) {
            Random random = new Random();
            int x = random.nextInt(screenSize.width);
            XueHua hua = new XueHua();
            hua.x = x;
            hua.y = y;
            hua.wind = WindType[random.nextInt(2)];
            hua.windSpeed = hua.wind * 3;
            hua.img = xueHuaImages[random.nextInt(xueHuaImages.length)];
            hua.x += hua.img.getWidth(null);
            if (hua.x >= screenSize.width) {
                hua.x = screenSize.width - hua.img.getWidth(null);
            }
            return hua;
        }
     
        public void run() {
            while (true) {
                try {
                    Thread.sleep(50);
                    down();
                    repaint();
                } catch (InterruptedException e) {
                }
            }
        }
     
        protected void down() {
            if (lock.tryLock()) {
                try {
                    for (Iterator<XueHua> it = list.iterator(); it.hasNext();) {
                        XueHua xh = it.next();
                        if (xh.y > screenSize.height) {
                            it.remove();
                            continue;
                        }
                        if (xh.x < 0 || xh.x > (screenSize.width - xh.img.getWidth(null))) {
                            xh.wind *= -1;
                            xh.windSpeed *= -1;
                        }
                        xh.x += xh.windSpeed;
                        xh.y += 3;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
     
        class XueHua {
            public int x;
            public int y;
            public int wind;
            public int windSpeed;
            public Image img;
        }
     
        public static void main(String[] args) {
            Thread start = new Thread(new MerryChristmas());
            start.start();
        }
    }

  • 相关阅读:
    【DFS】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem D. Divisibility Game
    【二分】【三分】【计算几何】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem L. Lines and Polygon
    【线段树】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem J. Jedi Training
    【贪心】【后缀自动机】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem E. Enter the Word
    【转载】随机生成k个范围为1-n的随机数,其中有多少个不同的随机数?
    【推导】【贪心】XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016 Problem H. Path or Coloring
    【枚举】XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016 Problem D. Cutting Potatoes
    【找规律】【递归】XVII Open Cup named after E.V. Pankratiev Stage 4: Grand Prix of SPb, Sunday, Octorber 9, 2016 Problem F. Doubling
    【贪心】Codeforces Round #436 (Div. 2) D. Make a Permutation!
    【计算几何】【圆反演】计蒜客17314 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 G. Finding the Radius for an Inserted Circle
  • 原文地址:https://www.cnblogs.com/xkzy/p/3920092.html
Copyright © 2011-2022 走看看