圣诞快乐java代码,java版的下雪,大家圣诞快乐

everybody, Merry Christmas ! 里面有播放音乐,太大了上传不了,想看效果的把播放音乐的代码注释掉就好了。

不好意思,图片忘记了,现补上了,不过图片是网上随便找的图扣的,不是很好看呐。。

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 list;

private Lock lock;

public MerryChristmas() {

list = new Vector();

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 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"));

}

} 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 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();

}

}

2.[文件] images.zip ~ 43KB     下载(402)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值